作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为游戏编写人工智能程序,并且希望能够为玩家设置不同的模式。
这是我在 Type.java 中的枚举:
public enum Type {
Human,Random,Minimax
}
这是player.java中用于设置播放器类型的构造函数:
public Player(String name, Type e ) {
this.name = name;
this.Type = e;
}
现在 Eclipse 说“类型无法解析或不是字段。”我应该怎么办?两个文件位于同一个包中。
最佳答案
Now Eclipse says "Type cannot be resolved or is not a field."
这告诉您问题出在行中的 Type
上:
this.Type = e;
// ^---- This one
如果您还没有在 Player
中声明一个字段:
private Type type;
...然后确保您在构造函数中使用该字段的名称:
this.type = e;
<小时/>
注意我使用小写字母作为字段名称。这是 Java 中的压倒性约定,并且与您对字段 name
所做的操作相匹配。
关于java - 编写构造函数,从不同的类获取枚举参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33464849/
我是一名优秀的程序员,十分优秀!