在以下场景中:
class Person{
public int ID;
}
class Student extends Person{
public int ID;
}
学生“隐藏人的ID字段。
如果我们想在内存中表示以下内容:
Student john = new Student();
john 对象是否有两个单独的内存位置用于 storint Person.ID 和它自己的?
正确的。你的例子中的每个类都有自己的int ID
id 字段。
您可以通过这种方式从子类中读取或分配值:
super.ID = ... ; // when it is the direct sub class
((Person) this).ID = ... ; // when the class hierarchy is not one level only
或者在外部(当它们是公开的时):
Student s = new Student();
s.ID = ... ; // to access the ID of Student
((Person) s).ID = ... ; to access the ID of Person
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)