You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java面向对象编程条件实例化问题求助:对象对战场景

Solution for Conditional Object Instantiation & Battle Setup

Hey there! Let's work through your problem with conditional instantiation and getting those playerAnimal and animalSauvage objects ready for battle. I spot two key issues in your current code, so let's break this down step by step.

1. First: Fix the Variable Scope Problem

Right now, your playerChien, playerChat, etc. variables are declared inside each switch case block—that means they don't exist outside the switch, so when you try to pass them to Fight, the compiler won't recognize them.

We need to declare the parent type variables before the switches, then assign them inside the cases:

// Declare variables outside the switches so they're accessible later
Animaux playerAnimal = null;
Animaux animalSauvage = null;

// Player's chosen animal
switch (animalChosen) { 
    case "Chien": 
        playerAnimal = new Chien(animalName); 
        playerAnimal.displayStat(); 
        break; 
    case "Chat": 
        playerAnimal = new Chat(animalName); 
        playerAnimal.displayStat(); 
        break; 
    case "Furet": 
        playerAnimal = new Furet(animalName); 
        playerAnimal.displayStat(); 
        break; 
    default: 
        // Add a fallback to handle invalid inputs
        System.out.println("Animal inconnu !");
        break; 
}

// Wild animal
switch (randAnimalSauvage()) { 
    case "Chien": 
        animalSauvage = new Chien(); 
        animalSauvage.displayStat(); 
        break; 
    case "Chat": 
        animalSauvage = new Chat(); 
        animalSauvage.displayStat(); 
        break; 
    case "Furet": 
        animalSauvage = new Furet(); 
        animalSauvage.displayStat(); 
        break; 
    default: 
        System.out.println("Animal sauvage inconnu !");
        break; 
}

// Now we can use these variables (add null checks to avoid crashes!)
if (playerAnimal != null && animalSauvage != null) {
    Fight letsFight = new Fight(playerAnimal, animalSauvage); 
    letsFight.battle(playerAnimal, animalSauvage); 
} else {
    System.out.println("Impossible de lancer le combat : un animal est invalide !");
}

2. Second: Reduce Duplication with a Factory Pattern

You're repeating almost identical switch logic for both player and wild animals. Let's clean this up with a Factory Class—this will centralize the object creation logic, making your code easier to maintain (if you add more animals later, you only change one place!).

Here's how to implement it:

// Factory class to handle Animaux instantiation
public class AnimauxFactory {
    // For player animals (with name parameter)
    public static Animaux createPlayerAnimal(String type, String name) {
        switch (type) {
            case "Chien":
                return new Chien(name);
            case "Chat":
                return new Chat(name);
            case "Furet":
                return new Furet(name);
            default:
                return null;
        }
    }

    // For wild animals (no name parameter)
    public static Animaux createWildAnimal(String type) {
        switch (type) {
            case "Chien":
                return new Chien();
            case "Chat":
                return new Chat();
            case "Furet":
                return new Furet();
            default:
                return null;
        }
    }
}

Now your main code becomes much cleaner:

Animaux playerAnimal = AnimauxFactory.createPlayerAnimal(animalChosen, animalName);
Animaux animalSauvage = AnimauxFactory.createWildAnimal(randAnimalSauvage());

if (playerAnimal != null && animalSauvage != null) {
    playerAnimal.displayStat();
    animalSauvage.displayStat();
    Fight letsFight = new Fight(playerAnimal, animalSauvage); 
    letsFight.battle(playerAnimal, animalSauvage); 
} else {
    System.out.println("Erreur : animal invalide !");
}

Quick Notes

  • Null Checks: Always add null checks before using the objects—this prevents NullPointerException if an invalid animal type is passed.
  • Scalability: If you add more animals (like "Lapin" or "Renard"), you only need to update the factory class, not every switch in your code.
  • Variable Names: No worries about your French variable names—they're clear and make perfect sense for your context!

内容的提问来源于stack exchange,提问作者AznYouth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:29:13