Jos*_*per 0 java enums subclass
所以我有一个类文件,里面只有我的枚举,看起来像这样
public class FactionNames {
public enum Faction {AMITY, ABNEGATION, DAUNTLESS, ERUDITE, CANDOR};
}
Run Code Online (Sandbox Code Playgroud)
我有一个在构造函数中使用这些枚举的类,它看起来像这样
public Dauntless(String f, String l, int a, int ag, int end, Faction d) {
super(f, l, a, d);
if (ag >= 0 && ag <= 10) {
this.agility = ag;
} else {
this.agility = 0;
}
if (end >= 0 && end <= 10) {
this.endurance = end;
} else {
this.endurance = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,为了确保此类中的所有内容都正常工作,我想在驱动程序中创建一些 Dauntless 对象,但我不断收到这些错误
D:\Documents\Google Drive\Homework\1331
Test.java:3: error: cannot find symbol
Faction test;
^
symbol: class Faction
location: class Test
Test.java:4: error: cannot find symbol
test = Faction.DAUNTLESS;
^
symbol: variable Faction
location: class Test
2 errors
Run Code Online (Sandbox Code Playgroud)
我正在使用我的驱动程序,它看起来像这样。我的语法有问题吗?我不明白为什么我会收到这个错误。
public class Test {
public static void main(String[] args) {
Faction test;
test = Faction.DAUNTLESS;
Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, test);
Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, test);
Dauntless winner;
winner = joe.battle(vik);
System.out.println(winner);
}
}
Run Code Online (Sandbox Code Playgroud)
该enum类型Faction嵌套在顶级类中FactionNames。
public class FactionNames {
public enum Faction {AMITY, ABNEGATION, DAUNTLESS, ERUDITE, CANDOR};
}
Run Code Online (Sandbox Code Playgroud)
如果你想使用它的简单名称,你需要导入它
import com.example.FactionNames.Faction;
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用其限定名称
FactionNames.Faction test = FactionNames.Faction.DAUNTLESS;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10285 次 |
| 最近记录: |