Java-在子类中初始化超类变量吗? [英] Java -- Initializing superclass variables in subclasses?

查看:62
本文介绍了Java-在子类中初始化超类变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,例如,假设我有一个名为"Vehicle"的抽象类.Vehicle类除其他外具有一个未初始化的静态变量,称为wheels.我想做的是从Vehicle类扩展其他子类,例如"Motorcycle"和"Truck",并在这些子类中初始化车轮.

Okay, so, for example, let's say I have an abstract class called "Vehicle". The Vehicle class, has, among other things, a static variable called wheels, which is not initialized. What I want to do is have other subclasses extending from the Vehicle class, like "Motorcycle", and "Truck", and in these subclasses, have the wheels initialized.

代码:

public abstract class Vehicle {
    static int wheels; //number of wheels on the vehicle
}

但是以下方法不起作用:

But the below doesn't work:

public class Motorcycle extends Vehicle {
    wheels = 2;
}

有没有办法有效地做到这一点?

Is there a way to do this effectively?

谢谢所有到目前为止回答的人.我知道,与将实例全部放在单独的类中相比,制作实例可能是一种更好的方法,但是我不能完全理解Java的静态"部分,因此在这里我需要一些帮助.

Thank you to all the people who replied so far. I get that making instances is probably a better way to go than to put them all in separate classes, but I don't get the "static" part of java perfectly, so I need a little help here.

我要为程序执行的操作是为Motorcycle和Truck类提供单独的sprite,并且我希望它们是静态的,这样我每次创建a的实例时都不必重新加载图像摩托车或卡车.但是除此之外,它们将具有彼此几乎相同的属性,这就是为什么它们都将从Vehicle超类扩展而来的原因.

What I'm trying to do for my program is have separate sprites for the Motorcycle and Truck classes, and I want them to be static so that I won't have to reload the image every time I create an instance of a Motorcycle or Truck. Other than that, though, they'll have almost identical properties to each other, which is why they'll both be extending from the Vehicle superclass.

我能看到的另一种方法是,不只是在Vehicle类中声明sprite变量,而是在Motorcycle/Truck类中声明如下:

The only other way I can see this being done is by just not declaring the sprite variable at the Vehicle class, but at the Motorcycle/Truck class, like below:

public abstract class Vehicle {
//Other coding
}

public class Motorcycle extends Vehicle {
static BufferedImage sprite = //initialize image
//Other coding
}

public class Truck extends Vehicle {
static BufferedImage sprite = //initialize image
//Other coding
}

推荐答案

如果车轮"是静态的,则只有一个,它将同时应用于所有车辆.因此,三轮车,摩托车,18轮卡车和福特将具有相同数量的车轮.

If 'wheels' is static, there is only one and it will apply to all vehicles at the same time. So tricycle, a motorcycle, an 18-wheeler truck and a Ford will all have the same number of wheels.

对我来说这没有意义.最好将"wheels"作为父类中的一个实例变量,但每个子类都应进行适当设置.

That doesn't make sense to me. It would be better to have 'wheels' be an instance variable that is in the parent class but each subclass sets appropriately.

但是您可以尝试

Vehicle.wheels = 2;

注意:自您添加了问题以来,我正在添加我的答案.

NOTE: I'm adding to my answer since you added to your question.

我喜欢您在每个子类中都具有静态变量的想法.但是您应该将它们设为私有.然后在

I like your idea of having statics in each of the subclasses. But you should make them private. Then put an abstract method in the parent class (Vehicle) like

public abstract BufferedImage getSprite();

然后,每个直接子类必须具有相同的方法,并且它可以返回私有静态变量.

Then each direct subclass has to have the same method and it can return the private static variable.

将变量设为静态,因此只需加载一次即可.将它们设为私有,以使类外部的代码无法自欺欺人,并引入错误.如果可能的话,您可以将它们设置为最终的",这样,类本身中的代码就无法在更改事实并引入错误后对其进行更改.('final'变量不能更改其值,但其值的内容可以更改.因此,'final'并非如其所愿.)

Make the variable static so you only have to load them once. Make them private so that code outside the class itself can't fool with it and introduce bugs. You could make them 'final' if possible so the code in the class itself can't change it after the fact and introduce bugs. (A 'final' variable can't have its value changed but the contents of its value can change. So 'final' isn't a wonderful as it could be.)

这篇关于Java-在子类中初始化超类变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆