简体   繁体   English

使用重写的抽象方法从超类访问私有变量

[英]Accessing private variables from a super class using an overridden abstract method

Suppose an abstract super class contains a private variable called price of type double.假设一个抽象超类包含一个名为 price 的 double 类型的私有变量。

Now suppose the variable has been declared but not initialized.现在假设变量已声明但未初始化。 The super class contains accessor methods, however the setter method is abstract, thus it must be overridden in a sub-class, but, as the super variable is private, is there any way to initialize this variable from a sub-class?超类包含访问器方法,但是 setter 方法是抽象的,因此必须在子类中覆盖它,但是,由于超变量是私有的,有没有办法从子类初始化这个变量?

Consider the following example: I have 4 Classes;考虑以下示例:我有 4 个类; Book(abstract super), NonFiction(sub), Fiction(sub), Tester(class to test what happens).书(抽象超级),非小说(子),小说(子),测试员(类来测试会发生什么)。

The Book Class:图书类:

public abstract class Book {

private String title;
private double price;

Book(String name){
    title = name;
}

public String getTitle(){
    return title;
}

public double getPrice(){
    return price;
}

public abstract void setPrice(double cost);
}

The Fiction Class小说类

public class Fiction extends Book{

Fiction(){
    super("Harry-Potter");
    setPrice(24.99);
}

@Override
public void setPrice(double cost) {
}
}

The NonFiction Class非小说类

public class NonFiction extends Book {公共类 NonFiction 扩展 Book {

NonFiction(){
    super("Head-first Java");
    setPrice(37.99);
}

public void setPrice(double cost) { 
}
}

The Tester Class测试员类

public class Challenge {
public static void main(String[] args){


Fiction fictionBook = new Fiction();
NonFiction NonFictionBook = new NonFiction();

Book[] theList = new Book[2];
theList[1] = NonFictionBook;
theList[0] = fictionBook;

System.out.println("Title of fiction book: \n"+theList[0].getTitle());
System.out.println("Title of non fiction book \n"+theList[1].getTitle());   
System.out.println("Title - "+theList[0].getTitle()+" -" + theList[0].getPrice());
System.out.println("Title - "+theList[1].getTitle()+" -" + theList[1].getPrice());
}

}

The expected output should be:预期的输出应该是:

Title of fiction book: Harry Potter Title of non fiction book: Head-First Java Title-Harry Potter Cost-$24.99 Title-Head-First Java Cost-$37.99小说书名:哈利波特非小说书名:Head-First Java Title-Harry Potter Cost-$24.99 Title-Head-First Java Cost-$37.99

Is there a way to access the private variable without changing the scope to protected?有没有办法在不改变保护范围的情况下访问私有变量?

The point of making a variable private is to prevent anything outside the class from accessing or changing it.将变量设为私有的目的是防止类外的任何内容访问或更改它。 If the developer of the class wanted it to be viewable or changeable by subclasses it would have been made protected.如果类的开发人员希望它可以被子类查看或更改,它就会受到保护。 However, it is possible to change the value using reflection (as long as the security manager is not configured to disallow that).但是,可以使用反射来更改该值(只要安全管理器未配置为禁止这样做)。 See How do I read a private field in Java?请参阅如何在 Java 中读取私有字段?

(Btw, just to nitpick, if you don't initialize an instance variable it gets a default value set for it, price will be set to 0.0D if you don't assign it an initial value. Leaving an instance or class variable uninitialized isn't really an option.) (顺便说一句,只是吹毛求疵,如果你不初始化一个实例变量,它会为它设置一个默认值,如果你不给它分配一个初始值, price将设置为0.0D 。让实例或类变量未初始化不是一个真正的选择。)

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

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