如何访问与子变量同名且子引用在子 class 之外的父 class 变量?
How to access parent class variable having same name as child variable with child reference outside the child class?
有没有办法通过子 class 外部的子引用访问与另一个子 class 实例变量同名的父 class 实例变量?
class Parent {
int i;
}
class Child extends Parent {
int i=10;
}
class Test {
public static void main(String[] args) {
Parent p=new Parent();
Child c=new Child();
System.out.println(p.i);//parent i variable
System.out.println(c.i);//child i variable
System.out.println(c.i);// again child i variable
}
}
假设有充分的理由,那么是的:
class Child extends Parent {
int i=10;
public int getParentsI() {
return super.i;
}
}
现在您的主要方法将如下所示:
Parent p=new Parent();
Child c=new Child();
System.out.println(p.i);//parent i variable
System.out.println(c.i);//child i variable
System.out.println(c.getParentsI());// parent i variable
编辑:意识到用户可能是新用户,所以我将充分充实方法 sig 并发表更多评论
将 Child
转换为 Parent
:
System.out.println(((Parent) c).i);
为什么有效?
一个 Child
实例有两个名为 i
的字段,一个来自 Parent
class,一个来自 Child
,编译器(不是实例的运行时类型)决定使用哪一个。编译器根据他看到的类型来做这件事。因此,如果编译器知道它是一个 Child
实例,他将为 Child
字段生成一个访问器。如果他只知道它是 Parent
,您就可以访问 Parent
字段。
一些例子:
Parent parent = new Parent();
Child child = new Child();
Parent childAsParent = child;
System.out.println(parent.i); // parent value
System.out.println(child.i); // child value
System.out.println(((Parent) child) .i); // parent value by inline cast
System.out.println(childAsParent.i); // parent value by broader variable type
如果编译器知道它是一个 Child
,他会授予对 Child
字段的访问权限,如果你拿走这个知识(通过强制转换或存储到 Parent
变量中) ,您可以访问 Parent
字段。
这很令人困惑,不是吗? 这是对各种令人讨厌的误解和编码错误的邀请。因此,最好不要在父项和子项中使用相同的字段名称 class.
有没有办法通过子 class 外部的子引用访问与另一个子 class 实例变量同名的父 class 实例变量?
class Parent {
int i;
}
class Child extends Parent {
int i=10;
}
class Test {
public static void main(String[] args) {
Parent p=new Parent();
Child c=new Child();
System.out.println(p.i);//parent i variable
System.out.println(c.i);//child i variable
System.out.println(c.i);// again child i variable
}
}
假设有充分的理由,那么是的:
class Child extends Parent {
int i=10;
public int getParentsI() {
return super.i;
}
}
现在您的主要方法将如下所示:
Parent p=new Parent();
Child c=new Child();
System.out.println(p.i);//parent i variable
System.out.println(c.i);//child i variable
System.out.println(c.getParentsI());// parent i variable
编辑:意识到用户可能是新用户,所以我将充分充实方法 sig 并发表更多评论
将 Child
转换为 Parent
:
System.out.println(((Parent) c).i);
为什么有效?
一个 Child
实例有两个名为 i
的字段,一个来自 Parent
class,一个来自 Child
,编译器(不是实例的运行时类型)决定使用哪一个。编译器根据他看到的类型来做这件事。因此,如果编译器知道它是一个 Child
实例,他将为 Child
字段生成一个访问器。如果他只知道它是 Parent
,您就可以访问 Parent
字段。
一些例子:
Parent parent = new Parent();
Child child = new Child();
Parent childAsParent = child;
System.out.println(parent.i); // parent value
System.out.println(child.i); // child value
System.out.println(((Parent) child) .i); // parent value by inline cast
System.out.println(childAsParent.i); // parent value by broader variable type
如果编译器知道它是一个 Child
,他会授予对 Child
字段的访问权限,如果你拿走这个知识(通过强制转换或存储到 Parent
变量中) ,您可以访问 Parent
字段。
这很令人困惑,不是吗? 这是对各种令人讨厌的误解和编码错误的邀请。因此,最好不要在父项和子项中使用相同的字段名称 class.