Java中的继承和重写

37 阅读1分钟

Pasted image 20230921130103.png

Pasted image 20230921133034.png

对父类的无参构造方法的调用,没有加super()编译器会自动加super(),即默认是调用无参构造方法。

Pasted image 20230921135151.png

Pasted image 20230921135434.png

Pasted image 20230921135713.png

调用super的有参构造方法,可以不用手动输入setName()和setAge()

Pasted image 20230921140437.png 直接子类和间接子类(对应直接父类和间接父类)

Pasted image 20230921142021.png

在子类中进行方法重写,写父类里已经有的方法,这里是show()

Pasted image 20230921143402.png

调用父类的方法:super.show()

Pasted image 20230921143722.png

@override标识符

Pasted image 20230921143906.png

子类重写,方法的访问权限不能变小,可以相同或者变大(如本来方法在父类是public,在子类不能变成private)

Pasted image 20230921144606.png