gpt4 book ai didi

java - 将常量分配给对象

转载 作者:行者123 更新时间:2023-12-02 03:09:22 24 4
gpt4 key购买 nike

我有一个抽象类 Animal,它有几个子类,例如 Dog、Cat 和 Rabbit。

每个子类都使用 Animal 类的构造函数,但子类中的每个对象也应该需要这样的常量。 Dog 对象应始终具有animalNumber = 1,并且每个Dog 对象应具有maximumNumberOfAnimals = 5 属性。但每个Cat 对象应始终具有animalNumber = 2,并且每个Cat 对象应具有maximumNumberOfAnimals = 10 属性。

我该如何编码?

这是我到目前为止所拥有的:

这是父类(super class):

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 无法分配但我也认为这是行不通的。有什么建议吗?

提前致谢!

最佳答案

So for every Dog, the ANIMALNUMBER should be 1, for every Cat, the ANIMALNUMBER should be 2.

你把事情搞得太复杂了。最简单的解决方案是删除构造函数参数并保留 ANIMALNUMBER 不变。

但是

为子类定义 ID 意味着您稍后计划根据动物的 ANIMALNUMBER 执行一些特殊操作。

当我们进行 OOP 时,这不是我们想要的。

您以后想要基于ANIMALNUMBER做的事情应该在具体的“Animal”实现中的方法中完成(例如Dog),覆盖类中的抽象方法动物

 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 "";
}
<小时/>

[编辑]

I will place these animals in a grid at some point. And I would like to count the number of Dogs, Cats and Rabbits I have.

我必须承认这是某种极端情况......

Can i count them based on their class?

是的,通过使用 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));
}
}

关于java - 将常量分配给对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280480/

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