Java – 在子类中使用super时属性丢失

huangapple go评论86阅读模式
英文:

Java - attribute missing when using super in child class

问题

我正在创建一些Java代码,其中包含一个Flying Objects数组,所有这些对象都有一个属性:价格(double)。我对继承有些陌生,这是我第一次使用super关键字。当我将子类对象Airplane创建到数组中时,价格特性似乎无法正确地通过构造函数传递。

以下是我的FlyingObject构造函数:

public class FlyingObject {

    protected double price;
    
    public FlyingObject() {
        price = 0;
    }
    
    public FlyingObject(double aPrice) {
        price = aPrice;
    }
}

以下是我的Airplane构造函数:

// 构造函数

public Airplane() {
    super();
    brand = "Unknown brand";
    horsepower = 0;
}

public Airplane(String aBrand, double aPrice, int aHorsepower) {
    super(aPrice);
    brand = aBrand;
    horsepower = aHorsepower;
}

每当我使用String、double和int参数创建飞机时,字符串(品牌)和整数(马力)都会被正确地注册,但价格仍保持为0。我是否明显地做错了什么,而我却忽略了?任何帮助将不胜感激。

编辑:找到了我做错的地方。愚蠢的错误。

在我的Airplane类中,我重新定义了价格作为实例变量,并且忘记了这一点,它覆盖了来自FlyingObjects的价格。

当我去掉那个额外的价格变量,只从超类(如预期)继承价格变量时,一切都正常工作了。

下次会发布更好的示例(可复现的)。干杯

英文:

I'm creating some Java code of an array of Flying Objects, which all have an attribute price (double). I am somewhat new to inheritance and this is is my first time using the super keyword. When I create a child class object Airplane into the array, the price feature does not seem to properly pass through the constructor.

Here are my FlyingObject constructors:

public class FlyingObject {

protected double price;

public FlyingObject()
{
	price = 0;
}

public FlyingObject(double aPrice)
{
	price = aPrice;
}

and here are my Airplane constructors:

// CONSTRUCTORS

public Airplane ()
{
	super();
	brand = "Unknown brand";
	horsepower = 0;
}

public Airplane (String aBrand, double aPrice, int aHorsepower)
{
	super(aPrice);
	brand = aBrand;
	horsepower = aHorsepower;
}

Everything when I create the airplane with arguments String, double and int, the string (brand) and int (horsepower) get registered correctly, but the price stays at 0. Is there something I am doing obviously wrong that I am missing? Any help would be greatly appreciated.

EDIT: Found what I did wrong.Silly mistake.

In my Airplane class, I had redefined price as an instance variable and had forgot about it, which overwrote the price from FlyingObjects.

As soon as I took out that extra price variable and only had the price variable come from the superclass (as intended) then everything worked fine.

Will post better examples (reproducible) next time. Cheers

答案1

得分: 1

我不太确定你遇到的问题是什么。我刚刚在jshell中尝试了一下,似乎运行得很好:

public class FlyingObject {
    protected double price;

    public FlyingObject() { price = 0; }

    public FlyingObject(double aPrice) { price = aPrice; }
}

public class Airplane extends FlyingObject {
    int horsepower;
    String brand;

    public Airplane() { 
        super(); 
        brand = "Unknown brand";
        horsepower = 0;
   }

   public Airplane(String aBrand, double aPrice, int aHorspower) {
        super(aPrice);
        brand = aBrand;
        horsepower = aHorsepower;
   }
}

如果我调用 Airplane plane = new Airplane("Boeing", 500000, 800);,我会得到一个Airplane对象,其结果是 plane.price = 500000

另外只是顺带一提,你可能考虑一下(而不是使用 aHorsepoweraBrand 等)在你的构造函数中只使用 horsepowerbrand,然后使用 this 关键字,如下所示:

public Airplane(String brand, double price, int horsepower) {
    super(price);
    this.brand = brand;
    this.horsepower = horsepower;
}
英文:

I'm not really sure what the issue you're having is. I just tried it out in jshell, and it seems to work pretty well:

public class FlyingObject {
    protected double price;

    public FlyingObject() { price = 0; }

    public FlyingObject(double aPrice) { price = aPrice; }
}

public class Airplane extends FlyingObject {
    int horsepower;
    String brand;

    public Airplane() { 
        super(); 
        brand = "Unknown brand";
        horsepower = 0;
   }

   public Airplane(String aBrand, double aPrice, int aHorspower) {
        super(aPrice);
        brand = aBrand;
        horsepower = aHorsepower;
   }
}

If I then call Airplane plane = new Airplane("Boeing", 500000, 800); I get an Airplane object, the results of which are that plane.price = 500000.

Also just as an aside, you might want to consider (instead of aHorsepower, aBrand, etc.) just using horsepower and brand in your constructors, and then using the this keyword as follows:

public Airplane(String brand, double price, int horsepower) {
    super(price);
    this.brand = brand;
    this.horsepower = horsepower;
}

huangapple
  • 本文由 发表于 2020年10月19日 14:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64422300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定