- class Fu{
- int num = 4;
-
- }
- class Zi0 extends Fu{
- void show(){
- System.out.println("num = "+num);//这个num是子类从父类继承而来的。num是父类的,也是子类的。。都是同一个num
- System.out.println( "this.num = "+this.num);
- System.out.println( "super.num = "+super.num);
-
- /*打印结果
- num = 4
- this.num = 4
- super.num = 4
-
- */
- }
- }
- class Zi1 extends Fu{
- int num =5; //子类有与父类同名的变量num,就会把父类的num给隐藏了。
- void show(){
- System.out.println("num = "+num); //这里的num相当于this.num。这个num是自己的,父类的num被隐藏了。
- System.out.println( "this.num = "+this.num);
- System.out.println( "super.num = "+super.num);//如果要得到被隐藏的父类的num,应该使用super.num
-
- /* 打印结果
- num = 5
- this.num = 5
- super.num = 4
- */
- }
- }
- public class ExtendTest {
- public static void main(String[] args){
- Zi0 z0 = new Zi0();
- z0.show();
- System.out.println("------------------");
- Zi1 z1 = new Zi1();
- z1.show();
-
- }
- }
复制代码 |