在 Java 中的“if”语句中声明一个变量,该变量根据条件而具有不同的类型
我知道,我知道,有大量简单的答案涵盖了如何避免这种情况的大多数情况。
就我而言,我想使用用户输入信息在游戏中创建 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您的目的是仅调用 CpuPlayer 类可用的方法,那么也许更好的设计模式是 策略模式。在您的情况下,您可能会添加一个名为 CpuStrategy 的新类,并将您的 CpuPlayer 构造函数修改为以下内容:
这使得其余代码更易于阅读,也可能更易于维护。原始代码片段如下所示:
我们去掉了 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:
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:
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.
先声明,再赋值。
Declare it first, and then assign it.
您可以调整类结构的设计。创建一个名为 Level 的父类并扩展两个名为 EasyLevel 和 HardLevel 的子类。
在您的玩家类中,添加一个修改构造函数以采用 Level 类型的参数。
这将使您的类结构更具可扩展性。举例来说,如果您将来需要添加另一个级别“中”,则只需从父类级别扩展即可。
You could tweak the design of your class structure. Make a parent class named Level and extend two child classes named EasyLevel and HardLevel.
in your player class, add a modify the constructor to take a parameter of type Level.
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.
如果可能的话,
cpu
也可以成为final
。cpu
can be madefinal
if possible too.