简体   繁体   English

从抽象类访问私有变量,访问方法为抽象

[英]Accesing private variable from abstract class with access method as abstract

Let's say I have a class called book that is abstract, has a private variable price, and also has it's getter method as abstract.假设我有一个名为 book 的类,它是抽象的,有一个私有变量 price,并且它的 getter 方法也是抽象的。

    public abstract class Book{

        private double price;

        public abstract double getPrice();

    }

Now let's say I have a used Book class that inherits all of Book's attributes, but also has an age associated with it.现在假设我有一个使用过的 Book 类,它继承了 Book 的所有属性,但也有一个与之相关的年龄。 Also I want this class to override the getPrice method from it's parent class.另外我希望这个类从它的父类覆盖 getPrice 方法。 This is where I get stuck since the price variable is private, and the parent class has an abstract getter method.这是我卡住的地方,因为 price 变量是私有的,并且父类有一个抽象的 getter 方法。

    public class UsedBook extends Book{

        private int age;

        //constructor
        public UsedBook(){
            age = 1;
        }

        @Override
        public double getPrice(){
            //How can I implement this method so as a user I can access the price?
        }

    }

Thank You谢谢你

If all implementations of Book must implement getPrice() , and the implementation is always to just return the price, it should not be abstract.如果Book所有实现都必须实现getPrice() ,并且实现始终只是返回价格,则它不应该是抽象的。 You should just define the getter within your abstract class:您应该只在抽象类中定义 getter:

public abstract class Book{

    private double price;

    public double getPrice() {
        return price;
    }

}//Ends book class

If this doesn't work, and you need to directly access the price variable from inheriting classes, then you should change it to be protected instead of private .如果这不起作用,并且您需要从继承类直接访问price变量,那么您应该将其更改为protected而不是private

If you do not want the price field variable to be publicly accessible from the Abstract class then, you should change the access modifier from private to protected .如果您不希望从Abstract类公开访问price字段变量,则应将访问修饰符从private更改为protected

protected access modifier means that the field isn't publicly accessible via instance object. protected访问修饰符意味着该字段不能通过实例对象公开访问。 However any subclass that inherits the Abstract class directly or indirectly has the protected fields inherited as a field variable, and the same rules goes apply, it cannot be publicly accessible.然而,任何直接间接继承Abstract类的子类都将protected字段作为字段变量继承,同样的规则适用,它不能被公开访问。

So to wrapped it up, the Book class:所以总结一下, Book类:

public abstract class Book {
  protected double price;

  public abstract double getPrice();
}

The subclass that inherits the Book class:继承Book类的子类:

public class UsedBook extends Book{

  private int age;

  public UsedBook(){
    this.age = 1;
    this.price = 0; // You should also set the field variable from the abstract "Book" class
  }

  @Override
  public double getPrice(){
    return this.price;
  }
}

Mark price as protected then you can access to it from sub classes :将价格标记为受保护,然后您可以从子类访问它:

public abstract class Book{

    protected double price;

    public abstract double getPrice();

}

public class UsedBook extends Book{

...

    @Override
    public double getPrice(){
        return price;
    }

..

}

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

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