public static void main(String []args){
Father f = new Son();
System.out.println(f.x);
//f.show();
}
}
//父类
class Father{
int x = 20;
public void show(){
//int x = 60 ;
System.out.println( x );
}
}
class Son extends Father{
int x = 5;
public void show(){
//int x = 90 ;
System.out.println( x );
}
}
非静态成员变量:
编译时期,参考父类中的成员变量,如果有编译成功,没有编译失败
运行时期,参考父类中的成员变量
int x = 20;
int x = 5;
Father f = new Son();
System.out.println(f.x);
20
静态成员变量:
编译时期,参考父类中的成员变量,如果有编译成功,没有编译失败
运行时期,参考父类中的成员变量
Static int x = 20;
Static int x = 5;
Father f = new Son();
System.out.println(f.x);
20
非静态成员方法:
编译时期,参考父类中的成员方法,如果有编译成功,没有编译失败
运行时期,运行的是子类重写后的方法,如果子类没重写,运行父类的
public void show(){
//int x = 60 ;
System.out.println( x );
}
f.show(); -----> 5
静态成员方法:
编译时期,参考父类中的静态成员方法,如果有编译成功,没有编译失败
运行时期,运行父类中的静态成员方法
public static void show(){
//int x = 60 ;
System.out.println( x );
}
f.show(); -----> 20
简单的说:
除了非静态的成员方法以外,编译看左边,运行看左边 Fu f = new Zi() ; 等于号的左边
只有非静态的成员方法,编译看左边,运行看右边
看明白 你就知道运行原理作者: SyouRai_Tsk 时间: 2014-4-16 22:10
楼主是正确的.如果方法不是覆盖了父类.那就是一直在执行父类.则不合理作者: 天山 时间: 2014-4-16 22:18
package com.itheima;
// 这是多态啊, 你虽然定义一个 父母对象,可以指针确实孩子,孩子对象在调用时候,首先应该调用本身自count 方法,
public class debug_18_parent {
public void count(){
System.out.println(10%3);
}
}
package com.itheima;
public class debug_18_Child extends debug_18_parent {
public void count(){
System.out.println(10/3+":::");
}