1、对属性测试:
1-1、直接访问属性
MyFoo1.java
public class MyFoo1 {
private String parm1 = "1-1";
String parm2 = "1-2";
protected String parm3 = "1-3";
public String parm4 = "1-4";
}
MyFoo2.java
public class MyFoo2 extends MyFoo1 {
private String parm1 = "2-1";
String parm2 = "2-2";
protected String parm3 = "2-3";
public String parm4 = "2-4";
public static void main(String[] args) {
MyFoo2 mf2 = new MyFoo2();
System.out.println(mf2.parm1);
System.out.println(mf2.parm2);
System.out.println(mf2.parm3);
System.out.println(mf2.parm4);
MyFoo1 mf = new MyFoo2();
// System.out.println(mf.parm1);
System.out.println(mf.parm2);
System.out.println(mf.parm3);
System.out.println(mf.parm4);
}
}
输出
2-1
2-2
2-3
2-4
1-2
1-3
1-4
结论:obj.parm取的是定义类声明的属性,不论该属性的可见性。
1-2、通过方法访问:
测试1:
MyFoo1.java增加如下方法
private String getParm1_1(){
return parm1;
}
String getParm1_2(){
return parm1;
}
protected String getParm1_3(){
return parm1;
}
public String getParm1_4(){
return parm1;
}
private String getParm2_1(){
return parm2;
}
String getParm2_2(){
return parm2;
}
protected String getParm2_3(){
return parm2;
}
public String getParm2_4(){
return parm2;
}
private String getParm3_1(){
return parm3;
}
String getParm3_2(){
return parm3;
}
protected String getParm3_3(){
return parm3;
}
public String getParm3_4(){
return parm3;
}
private String getParm4_1(){
return parm4;
}
String getParm4_2(){
return parm4;
}
protected String getParm4_3(){
return parm4;
}
public String getParm4_4(){
return parm4;
}
MyFoo2.java的main改为
MyFoo2 mf2 = new MyFoo2();
// System.out.println(mf2.getParm1_1());
System.out.println(mf2.getParm1_2());
System.out.println(mf2.getParm1_3());
System.out.println(mf2.getParm1_4());
// System.out.println(mf2.getParm2_1());
System.out.println(mf2.getParm2_2());
System.out.println(mf2.getParm2_3());
System.out.println(mf2.getParm2_4());
// System.out.println(mf2.getParm3_1());
System.out.println(mf2.getParm3_2());
System.out.println(mf2.getParm3_3());
System.out.println(mf2.getParm3_4());
// System.out.println(mf2.getParm4_1());
System.out.println(mf2.getParm4_2());
System.out.println(mf2.getParm4_3());
System.out.println(mf2.getParm4_4());
System.out.println();
MyFoo1 mf = new MyFoo2();
// System.out.println(mf.getParm1_1());
System.out.println(mf.getParm1_2());
System.out.println(mf.getParm1_3());
System.out.println(mf.getParm1_4());
// System.out.println(mf.getParm2_1());
System.out.println(mf.getParm2_2());
System.out.println(mf.getParm2_3());
System.out.println(mf.getParm2_4());
// System.out.println(mf.getParm3_1());
System.out.println(mf.getParm3_2());
System.out.println(mf.getParm3_3());
System.out.println(mf.getParm3_4());
// System.out.println(mf.getParm4_1());
System.out.println(mf.getParm4_2());
System.out.println(mf.getParm4_3());
System.out.println(mf.getParm4_4());
输出
1-1
1-1
1-1
1-2
1-2
1-2
1-3
1-3
1-3
1-4
1-4
1-4
1-1
1-1
1-1
1-2
1-2
1-2
1-3
1-3
1-3
1-4
1-4
1-4
测试2:MyFoo2中添加与MyFoo1一样的方法
输出
2-1
2-1
2-1
2-2
2-2
2-2
2-3
2-3
2-3
2-4
2-4
2-4
2-1
2-1
2-1
2-2
2-2
2-2
2-3
2-3
2-3
2-4
2-4
2-4
2、对方法测试
2-1、直接调用方法
在1的基础上,给MyFoo1添加如下代码
private String getParm1_1_1(){
return getParm1_1();
}
String getParm1_2_1(){
return getParm1_1();
}
protected String getParm1_3_1(){
return getParm1_1();
}
public String getParm1_4_1(){
return getParm1_1();
}
private String getParm1_1_2(){
return getParm1_2();
}
String getParm1_2_2(){
return getParm1_2();
}
protected String getParm1_3_2(){
return getParm1_2();
}
public String getParm1_4_2(){
return getParm1_2();
}
private String getParm1_1_3(){
return getParm1_3();
}
String getParm1_2_3(){
return getParm1_3();
}
protected String getParm1_3_3(){
return getParm1_3();
}
public String getParm1_4_3(){
return getParm1_3();
}
private String getParm1_1_4(){
return getParm1_4();
}
String getParm1_2_4(){
return getParm1_4();
}
protected String getParm1_3_4(){
return getParm1_4();
}
public String getParm1_4_4(){
return getParm1_4();
}
MyFoo2.java的main改为
MyFoo2 mf2 = new MyFoo2();
// System.out.println(mf2.getParm1_1_1());
System.out.println(mf2.getParm1_2_1());
System.out.println(mf2.getParm1_3_1());
System.out.println(mf2.getParm1_4_1());
// System.out.println(mf2.getParm1_1_2());
System.out.println(mf2.getParm1_2_2());
System.out.println(mf2.getParm1_3_2());
System.out.println(mf2.getParm1_4_2());
// System.out.println(mf2.getParm1_1_3());
System.out.println(mf2.getParm1_2_3());
System.out.println(mf2.getParm1_3_3());
System.out.println(mf2.getParm1_4_3());
// System.out.println(mf2.getParm1_1_4());
System.out.println(mf2.getParm1_2_4());
System.out.println(mf2.getParm1_3_4());
System.out.println(mf2.getParm1_4_4());
System.out.println();
MyFoo1 mf = new MyFoo2();
// System.out.println(mf.getParm1_1_1());
System.out.println(mf.getParm1_2_1());
System.out.println(mf.getParm1_3_1());
System.out.println(mf.getParm1_4_1());
// System.out.println(mf.getParm1_1_2());
System.out.println(mf.getParm1_2_2());
System.out.println(mf.getParm1_3_2());
System.out.println(mf.getParm1_4_2());
// System.out.println(mf.getParm1_1_3());
System.out.println(mf.getParm1_2_3());
System.out.println(mf.getParm1_3_3());
System.out.println(mf.getParm1_4_3());
// System.out.println(mf.getParm1_1_4());
System.out.println(mf.getParm1_2_4());
System.out.println(mf.getParm1_3_4());
System.out.println(mf.getParm1_4_4());
输出
1-1
1-1
1-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
1-1
1-1
1-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
结论:调用方法是动态绑定的。
最终结论:属性&(静态|私有方法)是前期绑定的;其他方法是动态绑定的