class A{
public static void testP(){
System.out.println("A");
}
public void testM(){
System.out.println("X");
}
}class B extends A{
public static void testP(){
System.out.println("B");
}
public void testM(){
System.out.println("Y");
}
}public class Test1{
public static void main(String[] args){
A a=new A();
B b=new B();
a.testP();//结果是 A
b.testP();//结果是 B
a.testM();//结果是 X
b.testM();//结果是 Y
a=b;
a.testP();//结果是 A
b.testP();//结果是 B
a.testM();//结果是 Y
b.testM();//结果是 Y
}
}
既然a=b,为什么a.testP()和b.testP()的结果不一样?
public static void testP(){
System.out.println("A");
}
public void testM(){
System.out.println("X");
}
}class B extends A{
public static void testP(){
System.out.println("B");
}
public void testM(){
System.out.println("Y");
}
}public class Test1{
public static void main(String[] args){
A a=new A();
B b=new B();
a.testP();//结果是 A
b.testP();//结果是 B
a.testM();//结果是 X
b.testM();//结果是 Y
a=b;
a.testP();//结果是 A
b.testP();//结果是 B
a.testM();//结果是 Y
b.testM();//结果是 Y
}
}
既然a=b,为什么a.testP()和b.testP()的结果不一样?
b.testP() 等价于 b.getClass().testP(),即B.testP()
http://topic.csdn.net/u/20090203/10/3b55ea03-c388-48f7-8574-578b25b07e58.html
比如A为静态方法,a.testP合法不?