根据此在线自学assignment中提供的course,
我想存储int hungerLevel
& int starvationTime
仅适用于Shark
级以下的TypeAndSize
,不适用于Fish
或Empty
。
class TypeAndSize {
int type; // runType EMPTY, SHARK, or FISH
int size; // Number of cells in the run for that runType.
public static short EMPTY = 0;
public static short SHARK = 1;
public static short FISH = 2;
/**
* Constructor for a TypeAndSize of specified species and run length.
* @param species is Ocean.EMPTY, Ocean.SHARK, or Ocean.FISH.
* @param runLength is the number of identical cells in this run.
* @return the newly constructed Critter.
*/
TypeAndSize(int species, int runLength) {
if ((species != TypeAndSize.EMPTY) &&
(species != TypeAndSize.FISH) &&
(species != TypeAndSize.SHARK)) {
System.out.println("TypeAndSize Error: Illegal species.");
System.exit(1);
}
if (runLength < 1) {
System.out.println("TypeAndSize Error: runLength must be at least 1.");
System.exit(1);
}
this.type = species;
this.size = runLength;
}
}
我的问题:
从语法上讲,Java是否允许您仅为Shark
类中的TypeAndSize
声明这两个成员,?
注意:我是Java编程的新手。