gpt4 book ai didi

java - 编写构造函数,从不同的类获取枚举参数

转载 作者:行者123 更新时间:2023-12-02 04:14:29 26 4
gpt4 key购买 nike

我正在尝试为游戏编写人工智能程序,并且希望能够为玩家设置不同的模式。

这是我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com