英文:
Required class variables in Java?
问题
以下是翻译好的部分:
假设,为了举例说明,我有一个抽象类Animal,每个Animal都有numberOfLegs。
Dog扩展Animal,所有狗都有4条腿的numberOfLegs。
是否可以使numberOfLegs...
- 静态
- 必需的(扩展Animal的所有内容都必须定义numberOfLegs,否则有一些默认值)
- 从Animal可访问(这一点我不太担心,但如果从Dog调用walk()方法,Animal是否可以查看腿的数量而不必传递额外的值?)
如果这个问题有点奇怪,我很抱歉,是的,我明白我可以用其他方式轻松实现这个问题,例如将其作为实例变量而不是静态的,但我只是好奇是否有更好的方法。
英文:
So say, for the purposes of illustration that I have an abstract class Animal, and every Animal has a numberOfLegs.
Dog extends Animal and all dogs have a numberOfLegs of 4.
Is it possible to make numberOfLegs...
- Static
- Required (everything that extends Animal has to define numberOfLegs, or there is some default value)
- Accessible from Animal (this one I'm not as concerned about, but if say the method walk() was called from Dog, could Animal view the number of legs without having to pass an extra value?)
Sorry if this is a strange question, and yes, I understand that I could do this easily in other ways, such as making it an instance variable instead of static, but I'm just kind of curious if there's a better way.
答案1
得分: 3
静态变量和实例变量都不参与多态性。只需在抽象类中声明一个抽象方法,例如 int getNumberOfLegs()
。你的 Dog
类可以是:
class Dog extends Animal {
private static final int NUMBER_OF_LEGS = 4;
public int getNumberOfLegs () {
return NUMBER_OF_LEGS;
}
}
英文:
Neither static variables or instance variables participate in polymorphism. Just declare an abstract method, e.g. int getNumberOfLegs()
, in the abstract class. Your Dog
class could be:
class Dog extends Animal {
private static final int NUMBER_OF_LEGS = 4;
public int getNumberOfLegs () {
return NUMBER_OF_LEGS;
}
}
答案2
得分: 2
> 是否可以将numberOfLegs设置为静态
这将使扩展Animal类的每个实例都具有相同的numberOfLegs。参见:这里以获取解释。
public abstract class Animal
{
private static int NUMBER_OF_LEGS = 4;
}
> 是否可以将numberOfLegs设置为必需
是的,您只需在抽象类中创建一个属性,并在构造函数中进行初始化。
public abstract class Animal
{
private int numberOfLegs;
public int getNumberOfLegs()
{
return this.numberOfLegs;
}
public Animal(int numberOfLegs)
{
this.numberOfLegs = numberOfLegs;
}
}
public class Zebra extends Animal
{
public Animal(int numberOfLegs)
{
super(numberOfLegs);
}
}
> 扩展Animal的所有内容都必须定义numberOfLegs,或者存在某个默认值
此外,如果您想要一个默认值作为腿的数量,您可以在Animal中包括一个没有numberOfLegs参数的构造函数,并将其设置为一个值,比如4。
public abstract class Animal
{
private int numberOfLegs;
public Animal()
{
this.numberOfLegs = 4;
}
}
> 是否可以从Animal中访问numberOfLegs
是的,您可以从扩展Animal的任何对象中调用该方法(如果在Animal类中是抽象的)。
Zebra z = new Zebra(4);
System.out.println(z.getNumberOfLegs());
英文:
> Is it possible to make numberOfLegs: Static
This would make every instance of a class that extends Animal have the same numberOfLegs. See: here for an explanation.
public abstract class Animal
{
private static int NUMBER_OF_LEGS = 4;
}
> Is it possible to make numberOfLegs: Required
Yes, you just have to make a property in the abstract class and initialise it in the constructor.
public abstract class Animal
{
private int numberOfLegs;
public int getNumberOfLegs()
{
return this.numberOfLegs;
}
public Animal(int numberOfLegs)
{
this.numberOfLegs = numberOfLegs;
}
}
public class Zebra extends Animal
{
public Animal(int numberOfLegs)
{
super(numberOfLegs);
}
}
> everything that extends Animal has to define numberOfLegs, or there is some default value
Furthermore, if you wanted a default value for number of legs, you could inlcude a constructor for Animal without a numberOfLegs parameter and set it to a value, say 4.
public abstract class Animal
{
private int numberOfLegs;
public Animal()
{
this.numberOfLegs = 4;
}
}
> Is it possible to make numberOfLegs: Accessible from Animal
Yes, you can call the method (if it's abstract in the Animal class) from any object that extends Animal
Zebra z = new Zebra(4);
System.out.println(z.getNumberOfLegs());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论