JAVA完美编程中写到:The derived class inherits all the public methods, all the public and private
instance variables, and all the public and private static variables from the base class and can
add more instance variables, more static variables, and more methods.
问题:如果父类有一个私有成员变量,那么子类也有着和父类相同名字的私有成员变量,因为他从父类中继承了PRIVATE INSTANCE VARIABLE?public class PrivateMethodInheritanceBaseClass {
private int variable1;
}
public class PrivateMethodInheritance extends PrivateMethodInheritanceBaseClass {
public static void main(String [] args){
PrivateMethodInheritance obj=new PrivateMethodInheritance();
//如果PrivateMethodInheritance类中也继承了VARIABLE1,那么如何访问这个变量?
//obj.variable1 is error!
}
}
instance variables, and all the public and private static variables from the base class and can
add more instance variables, more static variables, and more methods.
问题:如果父类有一个私有成员变量,那么子类也有着和父类相同名字的私有成员变量,因为他从父类中继承了PRIVATE INSTANCE VARIABLE?public class PrivateMethodInheritanceBaseClass {
private int variable1;
}
public class PrivateMethodInheritance extends PrivateMethodInheritanceBaseClass {
public static void main(String [] args){
PrivateMethodInheritance obj=new PrivateMethodInheritance();
//如果PrivateMethodInheritance类中也继承了VARIABLE1,那么如何访问这个变量?
//obj.variable1 is error!
}
}
解决方案 »
- 真心求帮助,这是我所有的分了。。。JAVA 关于部件的布局问题。。
- SCJP线程概念题目请教 谢谢
- 恳求:java中如何实现数字的集合的所有组合,并将所有数字的组合列举出来?谢谢!
- 一个java内码转换的问题
- 一条sql查询语句,请达人帮帮忙
- 菜鸟问题,在线等,马上送分!
- 我觉得在应用程序中,我们的应该尽量使class的方法和域不要成为static,因为static的方法和域在一进程当中是常驻内存,如果时间长了,会
- java.net.URL的openConnection()中如何确认链接已经打开了?
- 哪有unicode、中文转换表或程序?
- 请问哪些情况必须在数值的后面加上字符'L'或'l'?
- 快死人了。。。。。。。。。。。
- 菜鸟问两个关于drools flow 和jbpm的问题
import java.lang.reflect.Field;class PrivateMethodInheritanceBaseClass {
private int variable1;
public int getVariable1() {
return variable1;
}
}
public class PrivateMethodInheritance extends PrivateMethodInheritanceBaseClass {
public static void main(String [] args){
PrivateMethodInheritance obj=new PrivateMethodInheritance();
try {
Field field= PrivateMethodInheritanceBaseClass.class.getDeclaredField("variable1");
field.setAccessible(true);
field.setInt(obj, 10);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(obj.getVariable1());
//如果PrivateMethodInheritance类中也继承了VARIABLE1,那么如何访问这个变量?
//obj.variable1 is error!
}
}
A private class member or constructor is accessible only within the body of the top level class that encloses the declaration of the member or constructor. It is not inherited by subclasses. In the example:
class Point { Point() { setMasterID(); } int x, y; private int ID; private static int masterID = 0; private void setMasterID() { ID = masterID++; } }
the private members ID, masterID, and setMasterID may be used only within the body of class Point. They may not be accessed by qualified names, field access expressions, or method invocation expressions outside the body of the declaration of Point.
private的东西可以使用反射调用
但是看LZ的意思好像是误认为可以继承private的东西了