15_继承

 

 

 1.定义

class Zi extends Fu {
    //'age' has private access in 'JavaBasicReview.Fu'
    private int age /*= super.age*/;
    private String name = super.name;
    private int num = this.age;

    public void show() {
        System.out.println(name);//fu
        System.out.println(num);//0
    }
}

 


 

2.好处

 

 


 3.格式

 

 


 

 4.继承后特点

 

 


 

 5.super关键字

 

 


 6.构造方法

 

 


 

 7.继承的特点

 

 


 8.super和this的内存

 

 

 

 

 

 

class Fu {
    private int age = 10;
    public String name = "fu";

    public Fu getObj(){
        return this;
    }
}

class Zi extends Fu {
    //'age' has private access in 'JavaBasicReview.Fu'

    public Zi(int age) {
        this.age = age;
        //super();对super()的调用必须是构造函数体中的第一个语句
    }

    private int age /*= super.age*/;
    private String name = super.name;
    private int num = this.age;

    public void show() {
        System.out.println(name);//fu
        System.out.println(num);//0
    }
    //重写方法时,必须与父类方法 具有完全相同的方法名、参数列表,返回值类型必须小于或者等于父类返回值类型
    @Override
    public Zi getObj() {
        return this;
    }
}

 

 

 

posted @ 2020-05-12 18:06  学而不思则罔!  阅读(112)  评论(0)    收藏  举报