查询Java中类TypeAndSize的用法

时间:2014-11-02 22:49:47

标签: java oop

根据此在线自学assignment中提供的course

我想存储int hungerLevel& int starvationTime仅适用于Shark级以下的TypeAndSize,不适用于FishEmpty

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编程的新手。

0 个答案:

没有答案