在 Java 中的“if”语句中声明一个变量,该变量根据条件而具有不同的类型

发布于 2025-01-04 00:58:42 字数 618 浏览 3 评论 0原文

我知道,我知道,有大量简单的答案涵盖了如何避免这种情况的大多数情况。

就我而言,我想使用用户输入信息在游戏中创建 CPU 玩家。如果用户选择简单模式,那么我想声明并实例化 EasyPlayer 类的实例。否则,我想声明并实例化 HardPlayer 类的实例。无论哪种方式,变量的具体名称都需要是“cpu”,并且代码的其余部分不加区别地对“cpu”进行操作。也就是说,这些操作方式的所有差异都内置到它们不同的类中,这些类是 CpuPlayer 类的子类。

代码如下:

// Set the opponent.
if (difficulty == 0){
    EasyPlayer cpu = new EasyPlayer(num_rounds);
}
else{
    HardPlayer cpu = new HardPlayer(num_rounds);
}

这给了我一个令人烦恼的 cannot find symbol 错误。据我所知,每个人都说你不能在这样的条件内进行声明,因为范围问题以及它永远不会发生的可能性。

如果是这样,根据用户输入将单个变量声明为两个不同类之一的正确方法是什么?

I know, I know, there's a ton of simple answers that cover most cases for how to avoid this.

In my case, I want to use user-input info to create CPU players in a game. If the user chooses easy mode, then I want to declare and instantiate an instance of the EasyPlayer class. Otherwise, I want to declare and instantiate an instance of the HardPlayer class. Either way, the specific name of the variable needs to be "cpu" and the rest of the code operates on "cpu" indiscriminately. That is, all the differences in how these operate are built into their different classes, which subclass the CpuPlayer class.

So here's the code:

// Set the opponent.
if (difficulty == 0){
    EasyPlayer cpu = new EasyPlayer(num_rounds);
}
else{
    HardPlayer cpu = new HardPlayer(num_rounds);
}

This gives me the ever-annoying cannot find symbol error. From what I can read, everyone says you cannot make declarations inside a conditional like this due to scope problems and the possibility that it never occurs.

If so, what is the right way to alternatively declare a single variable as one of either of two different classes based on user input?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

思念满溢 2025-01-11 00:58:42
CpuPlayer cpu;

if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
}
else{
    cpu = new HardPlayer(num_rounds);
}
CpuPlayer cpu;

if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
}
else{
    cpu = new HardPlayer(num_rounds);
}
離人涙 2025-01-11 00:58:42

如果您的目的是仅调用 CpuPlayer 类可用的方法,那么也许更好的设计模式是 策略模式。在您的情况下,您可能会添加一个名为 CpuStrategy 的新类,并将您的 CpuPlayer 构造函数修改为以下内容:

public CpuPlayer(CpuStrategy strategy, int num_rounds)

这使得其余代码更易于阅读,也可能更易于维护。原始代码片段如下所示:

CpuPlayer cpu = new CpuPlayer(new CpuStrategy(difficulty), num_rounds);

我们去掉了 if/else,因为 CpuStrategy 类将处理难度级别之间的差异。这也是有道理的,因为您可以从程序的实质中抽象出“难度级别”的概念,我认为这是游戏的一部分。

If your intention is to call only methods available to the CpuPlayer class, then perhaps a better design pattern to use is the Strategy Pattern. In your case, you would probably add a new class called CpuStrategy, and modify your CpuPlayer constructor to something like:

public CpuPlayer(CpuStrategy strategy, int num_rounds)

This makes the rest of your code easier to read and probably easier to maintain too. Here's what your original snippet of code would look like:

CpuPlayer cpu = new CpuPlayer(new CpuStrategy(difficulty), num_rounds);

We got rid of the if/else since the CpuStrategy class will handle the difference between difficulty levels. This also makes sense since you can abstract away the notion of "difficulty levels" from the meat of your program, which I assume is the game playing part.

寂寞陪衬 2025-01-11 00:58:42
CpuPlayer cpu; 
// Set the opponent.
if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
} else{
    cpu = new HardPlayer(num_rounds);
}
CpuPlayer cpu; 
// Set the opponent.
if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
} else{
    cpu = new HardPlayer(num_rounds);
}
北城孤痞 2025-01-11 00:58:42

先声明,再赋值。

// Set the opponent.
CpuPlayer cpu = null;
if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
}
else{
    cpu = new HardPlayer(num_rounds);
}
if(cpu == null) throw new IllegalStateException();

Declare it first, and then assign it.

// Set the opponent.
CpuPlayer cpu = null;
if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
}
else{
    cpu = new HardPlayer(num_rounds);
}
if(cpu == null) throw new IllegalStateException();
燕归巢 2025-01-11 00:58:42
// Set the opponent.
CpuPlayer cpu;
if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
} else{
    cpu = new HardPlayer(num_rounds);
}
// Set the opponent.
CpuPlayer cpu;
if (difficulty == 0){
    cpu = new EasyPlayer(num_rounds);
} else{
    cpu = new HardPlayer(num_rounds);
}
盛夏已如深秋| 2025-01-11 00:58:42

您可以调整类结构的设计。创建一个名为 Level 的父类并扩展两个名为 EasyLevel 和 HardLevel 的子类。

class EasyLevel : public Level 
{

  //implementation specific to easy level
}

class HardLevel : public Level 
{
  //implementation specific to hard level
}

在您的玩家类中,添加一个修改构造函数以采用 Level 类型的参数。

class Player
{
  Level level;
  int rounds;

  public:
    Player (Level level, int num_rounds)
    {
       this.level = level;
       this.rounds = num_rounds;
    }

    play ()
    {
       // use level variable to invoke implementation specific to the set level, hard or easy. 
    }
}

这将使您的类结构更具可扩展性。举例来说,如果您将来需要添加另一个级别“中”,则只需从父类级别扩展即可。

You could tweak the design of your class structure. Make a parent class named Level and extend two child classes named EasyLevel and HardLevel.

class EasyLevel : public Level 
{

  //implementation specific to easy level
}

class HardLevel : public Level 
{
  //implementation specific to hard level
}

in your player class, add a modify the constructor to take a parameter of type Level.

class Player
{
  Level level;
  int rounds;

  public:
    Player (Level level, int num_rounds)
    {
       this.level = level;
       this.rounds = num_rounds;
    }

    play ()
    {
       // use level variable to invoke implementation specific to the set level, hard or easy. 
    }
}

This will make you class structure more extendable. As an example if you need to add another Level 'medium' in future, just need to extend from the parent class Level.

音盲 2025-01-11 00:58:42
CpuPlayer cpu = difficulty == 0
    ? new EasyPlayer(num_rounds)
    : new HardPlayer(num_rounds);

如果可能的话,cpu 也可以成为final

CpuPlayer cpu = difficulty == 0
    ? new EasyPlayer(num_rounds)
    : new HardPlayer(num_rounds);

cpu can be made final if possible too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文