简体   繁体   English

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

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

I want to assign the basePrice (which is inherited field from Hamburger class) to the double newPrice variable inside the overwritten addItems() method.我想将 basePrice(它是从 Hamburger 类继承的字段)分配给覆盖的 addItems() 方法内的双 newPrice 变量。 This was not a problem in the super class since the field existed.由于该领域存在,这在超类中不是问题。 I just typed "double newPrice = this.basePrice;".我只是输入“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???;
    }
}

To access this.basePrice from the derived class, basePrice attribute has to be protected in the base class.要从派生类访问this.basePrice ,必须在基类中protected basePrice属性。

Then you can do:然后你可以这样做:

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

Most likely there is a method called getBasePrice() which you should invoke:很可能有一个名为getBasePrice()的方法,您应该调用它:

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

If there isn't one, go to the source code of Hamburger ( Hamburger.java ) and add this method.如果没有,请到汉堡包的源代码( Hamburger.java )中添加此方法。

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).如果在HealthyBurger类中编写this.basePrice时出现错误,则意味着Hamburger类中的字段要么被标记为private ,要么它 BOTH [1] 在另一个包中,并且 [2] 已声明为没有访问修饰符(这是 java-ese 用于称为包私有的概念 - 意思是,只有包内的东西才能接触它,而你不在那里)。

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 .一种解决方法是进入Hamburger.java的源代码并使其protected (这意味着:此文件中的任何源代码、同一包中的所有类以及任何子类 - 最后一个将为你),但是将字段设为私有是更惯用的 Java,所以保持原样,并创建这个称为“getter”的方法,并且可能应该是public

NB: If you find it tedious to write these, there's always Lombok .注意:如果你觉得写这些很乏味,总有Lombok

You can just create a getBasePrice() method defined in the parent class or just declare the field:您可以只创建在父类中定义的getBasePrice()方法或仅声明该字段:

protected double basePrice

and you can just assign double newPrice = this.basePrice;你可以只分配double newPrice = this.basePrice;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM