![](/img/trans.png)
[英]accessing the private memeber variable of super class in the child class through reflection
[英]Accessing super class instance variable
专家在这里,我需要一些帮助以了解我无法理解的内容。 我正在创建一个超类和一个子类,我试图通过子类访问超类实例变量,但是我总是得到null。 除非我为超类中的'name'变量明确赋值。
package example1;
public class InterfaceExample {
public static void main(String[] args) {
Person person = new Person();
person.printinfo("WOLFSKIN", "Test Address");
Boy boy = new Boy();
boy.info();
}
}
class Person {
String name;
String address;
public void printinfo(String name, String address) {
this.name = name;
this.address = address;
System.out.println("Name: " + name + '\n' + "Address: " + address);
}
}
class Boy extends Person {
public void info() {
System.out.println("Subclass Name " + this.name);
}
}
您从未在Boy
实例中为name
分配任何值,我添加了一些解释性注释:
// Creates a Person (not a Boy)
Person person = new Person();
// Sets the name on that Person
person.printinfo("WOLFSKIN", "Test Address");
// Creates an *entirely separate object*, an instance of Boy
Boy boy = new Boy();
// Accesses `name`, which has never been set
boy.info();
要设置name
上你的Boy
实例,调用printinfo
你的Boy
实例:
boy.printinfo("WOLFSKIN", "Test Address");
让我们扔一些ASCII艺术:
Person person = new Person();
给我们:
+-----------+ +-----------------+ | person |---->| Person instance | +-----------+ +-----------------+ | name: null | +-----------+ | address: null | | boy: null | +-----------------+ +-----------+
然后:
person.printinfo("WOLFSKIN", "Test Address");
我们有:
+--------+ +-----------------+ +-----------------+ | person |------>| Person instance | | String instance | +--------+ +-----------------+ +-----------------+ | name |--->| "WOLFSKIN" | | address |-+ +-----------------+ +-----------------+ | +-----------+ | +-----------------+ | boy: null | | | String instance | +-----------+ | +-----------------+ +->| "Test Address" | +-----------------+
现在我们做:
Boy boy = new Boy();
并得到这个:
+--------+ +-----------------+ +-----------------+ | person |------>| Person instance | | String instance | +--------+ +-----------------+ +-----------------+ | name |--->| "WOLFSKIN" | | address |-+ +-----------------+ +-----------------+ | +-----------+ | +-----------------+ | boy |-+ | | String instance | +-----------+ | | +-----------------+ | +->| "Test Address" | | +-----------------+ | +-----------------+ +->| Boy instance | +-----------------+ | name: null | | address: null | +-----------------+
请注意,Boy实例由其超类的字段(在本例中为Person
)以及其自己的字段(但没有任何字段)组成。
由于您从未填写boy
name
的值,因此它仍然具有默认的null
。
您是从Boy
实例引用name
,但已在Person
实例中分配名称,因此它为null
。
您可以通过添加以下内容来消除该问题:
boy.printinfo("WOLFSKIN", "Test Address");
从你的榜样,您似乎认为, this
将返回您的超类,当它实际返回的类是在实例的实例。在您的例子, this
是指你的Boy
类及其字段,当你似乎想指Person
。 除非添加以下内容,否则Boy
与Person
name
。
Boy boy = new Boy();
boy.name = person.name;
boy.info();
我不是这方面的专家,但是我相信您也可以在info()
方法中做到这一点:
this.name = super.name;
System.out.println("Subclass Name " + this.name);
如果使用第二种方法,则可能需要在Person
中将name
设为静态变量,但是我不确定(如果有人知道,请编辑/评论)。 使用第一个可能更安全,因为这样您可以拥有多个Boy
实例,每个实例使用不同的名称。 对此内容有更多了解或知道我是否不正确的任何人,请编辑/评论任何更正。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.