我有一个抽象类Animal,它有几个子类,如Dog,Cat和Rabbit。
这些子类中的每一个都使用Animal类的构造函数,但子类中的每个对象也应该需要这样的常量。 Dog对象总是有animalNumber = 1,每个Dog对象都应该有一个属性maximumNumberOfAnimals = 5.但是每个Cat对象应该总是有animalNumber = 2,并且每个Cat对象都应该有一个属性maximumNumberOfAnimals = 10.
我该怎么编码呢?
这是我到目前为止所做的:
这是超类:
public abstract class Animal {
private int weight;
private int height;
public Animal (int weight, int height) {
setWeight(weight);
setHeight(height);
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
这是一个子类
public class Dog extends Animal {
public static final int ANIMALNUMBER= 1;
public static final int MAXIMUMNUMBERALLOWED= 5;
//the constructor
public Dog (int weight, int height, int animalNumber, int maximumNumberAllowed) {
super(weight, height)
Dog.animalNumber = ANIMALNUMBER;
Dog.maximumNumberAllowed = MAXIMUMNUMBERALLOWED;
}
}
这是另一个子类
public class Cat extends Animal {
public static final int ANIMALNUMBER= 2;
public static final int MAXIMUMNUMBERALLOWED= 10;
//the constructor
public Cat (int weight, int height, int animalNumber, int maximumNumberAllowed) {
super(weight, height)
Cat.animalNumber = ANIMALNUMBER;
Cat.maximumNumberAllowed = MAXIMUMNUMBERALLOWED;
}
}
我最终想要的是:
如果我打电话给构造函数使用Dog Lassie = new Dog(40,60,1,5)创建一个新的'Dog'。但我不应该这样做:Dog Lassie = new Dog(40,60,2,10)所以对于每只狗来说,ANIMALNUMBER应该是1,对于每只猫,ANIMALNUMBER应该是2.它是一个实例变量但是对于班级的每个实例,它应该是相同的。
这不仅表示:无法分配最终字段Dog.ANIMALNUMBER 但我也认为它不会起作用。 有什么建议吗?
提前致谢!
答案 0 :(得分:1)
因此,对于每只狗,ANIMALNUMBER应为1,对于每只猫,ANIMALNUMBER应为2。
你的事情过于复杂。最简单的解决方案是删除Constructor参数并保持ANIMALNUMBER
不变。
<强>但是强>
定义子类的ID意味着您以后计划在动物的ANIMALNUMBER
上做一些特殊的动作基础。
当我们进行OOP时,这不是我们想要的。
Wat,你后来想做的基于ANIMALNUMBER
的做法应该在具体的“动物”实现中的方法中完成(如Dog
)覆盖类Animal
中的抽象方法< / p>
public abstract class Animal {
// common fields
public Animal (int weight, int height) {
// initialize
}
public abstract sting giveSound();
}
public class Dog extends Animal {
@Override
public sting giveSound(){
return "Wooff";
}
public class Cat extends Animal {
@Override
public sting giveSound(){
return "Meaw";
}
public class Rabbit extends Animal {
@Override
public sting giveSound(){
return "";
}
<强> [编辑] 强>
我会在某些时候将这些动物放在网格中。而且我想算一下我的狗,猫和兔子的数量。
我必须承认这是某种角落案例......
我可以根据他们的班级来计算他们吗?
是的,使用Map
:
public abstract class Animal {
public addToCount(Map<String,Integer> counters){
// gets the class name of the child that has been called *new* on...
String myClassName = getClass().getName();
Integer oldCount = conters.getOrDefault(myClassName, Integer.ZERO);
conters.put(myClassName,oldCount.add(Integer.ONE));
}
}
答案 1 :(得分:1)
您可以将这两个属性添加到Animal
并从子类中初始化它们:
public abstract class Animal {
private int weight;
private int height;
private final int number;
private final int maximumAllowed;
public Animal(int weight, int height, int number, int maximumAllowed) {
setWeight(weight);
setHeight(height);
this.number = number;
this.maximumAllowed = maximumAllowed;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public final int getNumber() {
return number;
}
public final int getMaximumAllowed() {
return maximumAllowed;
}
}
public final class Dog extends Animal {
public Dog(int weight, int height) {
super(weight, height, 1, 5);
}
}
public final class Cat extends Animal {
public Cat(int weight, int height) {
super(weight, height, 2, 10);
}
}
答案 2 :(得分:0)
如果要在构造函数中设置常量值,则应将它们设置为非静态。 但是现在你有了全局常量,将animalNumber和maximumNumberAllowed传递给构造函数是没有意义的。您的常量将始终具有您设置的相同值:
public static final int MAXIMUMNUMBERALLOWED = 10;
Dog类的常量与Cat类的常量无关!