新程序员,需要帮助。将从超类继承的字段分配给子类内方法中的变量。

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

New programmer, need help. Assigning a field inherited from superclass to a variable inside a subclass' method

问题

我想将从Hamburger类继承的basePrice字段分配给覆盖的addItems()方法内的double newPrice变量。在超类中这不是问题,因为该字段是存在的。我只是键入了"double newPrice = this.basePrice;"。但是,在子类中这样做不起作用。我是编程新手,感谢任何帮助。

public class HealthyBurger extends Hamburger{

    public HealthyBurger(String meat, double basePrice) {
        super("Healthy Burger", "brown rye", meat, basePrice);
    }

    public void addItems() {
        double newPrice = this.basePrice;
    }
}
英文:

I want to assign the basePrice (which is inherited field from Hamburger class) to the double newPrice variable inside the overwritten addItems() method. This was not a problem in the super class since the field existed. I just typed "double newPrice = this.basePrice;". But, this is not working in the subclass. I'm new to programming, any help appreciated.

public class HealthyBurger extends Hamburger{

    public HealthyBurger(String meat, double basePrice) {
        super("Healthy Burger", "brown rye", meat, basePrice);
    }

    public void addItems() {
        double newPrice = ???HOW TO ASSISGN basePrice HERE???;
    }
}

答案1

得分: 1

为了从派生类中访问 this.basePrice,在基类中必须将 basePrice 属性设置为 protected

然后你可以这样做:

this.basePrice = ...; // 无论你想在这里做什么
英文:

To access this.basePrice from the derived class, basePrice attribute has to be protected in the base class.

Then you can do:

this.basePrice = ...; // whatever you want here

答案2

得分: 0

很可能有一个名为 getBasePrice() 的方法,你应该调用它:

this.getBasePrice();
// 而不是
this.basePrice;

如果没有这个方法,在 Hamburger (Hamburger.java) 的源代码中添加这个方法。

解释

如果在你的 HealthyBurger 类中写 this.basePrice 时出现错误,这意味着 Hamburger 类中的字段要么被标记为 private,要么同时满足以下两个条件:[1] 它位于另一个包中,以及 [2] 它被声明为没有访问修饰符(这是 Java 中的一个概念,称为 package private - 意味着只有包内的内容可以访问它,而你不在其中)。

修复的方法之一是进入 Hamburger.java 的源代码,并将其改为 protected(这意味着:此文件中的任何源代码、相同包中的所有类以及任何子类 - 最后一个将为你打开大门),但更符合 Java 习惯的方法是将字段设为私有,所以保持原样,并创建这个被称为 'getter' 的方法,应该将其声明为 public

注:如果你觉得编写这些方法很繁琐,总是可以使用 Lombok

英文:

Most likely there is a method called getBasePrice() which you should invoke:

this.getBasePrice();
// instead of
this.basePrice;

If there isn't one, go to the source code of Hamburger (Hamburger.java) and add this method.

Explanation

If you get an error when you write this.basePrice in your HealthyBurger class, that means the field in the Hamburger class is either marked as private or, it BOTH [1] is in another package, and [2] has been declared with no access modifier (which is java-ese for a concept called package private - meaning, only things inside the package can touch it, and you're not in there).

One fix is to go into the source code of Hamburger.java and make it protected instead (which means: Any source code in this file, all classes in the same package, and any subclasses - and that last one will open up the doors for you), but it is more idiomatic java to make fields private, so leave it as is, and make this method, which is called a 'getter', and should probably be public.

NB: If you find it tedious to write these, there's always Lombok.

答案3

得分: 0

你可以在父类中创建一个 getBasePrice() 方法,或者只需声明该字段:

protected double basePrice;

然后你可以直接赋值:double newPrice = this.basePrice;

英文:

You can just create a getBasePrice() method defined in the parent class or just declare the field:

protected double basePrice

and you can just assign double newPrice = this.basePrice;

huangapple
  • 本文由 发表于 2020年10月18日 18:16:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64412141.html
匿名

发表评论

匿名网友

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

确定