继承(is -a的关系)
一.背景
代码中创建的类主要是为了抽象现实中的一些事物。有时候客观的事物之间就会存在一定的关联关系,那么在表示成类和对象的时候也会存在一定的关联。
观察下列代码:
//定义动物类
class Animal{
public String name;
public Animal(String name){
this.name=name;
}
public void eat(){
System.out.println(this.name+"Animal:eat");
}
}
//定义猫的类
class Cat{
public String name;
public Cat(String name){
this.name=name;
}
public void eat(){
System.out.println(this.name+"Cat:eat");
}
}
//定义鸟的类
class Bird{
public String name;
public Bird(String name){
this.name=name;
}
public void eat(){
System.out.println(this.name+"Bird:eat");
}
public void fly(){
System.out.println(this.name+"Bird:fly");
}
}
上述代码中存在了大量的冗余代码,分析可以发现Animal,Cat,Bird这几个类中存在一定的关联关系:
①首先三个类都具有相同的属性name,并且意义是一样的
②其次,三个类具有一个相同的eat方法,而且行为是完全一样的
③从逻辑上讲,Cat和Bird是Animal的一种(is-a)
此时就可以让Cat和Bird分别继承Animal类,来达到代码复用的效果。像Animal这样的被继承的类,就叫父类/基类/超类。对于像Cat和Bird这样的类,称为子类/派生类。
注意:子类/派生类继承了父类中除构造方法外的所有东西,并且要先帮父类进行构造
二.语法规则
基本语法
class 子类 extends 父类{
}
①使用extends指定父类
②Java中一个子类只能继承一个父类(C++,Python等语言支持多继承)
③子类会继承父类所有public的字段和方法(构造方法除外)
④对于父类的private修饰的字段和方法,子类继承,但是无法访问
⑤子类的实例中,也包含父类的实例,可以使用super关键字得到父类实例的引用
super的用法
1.super() 调用父类的构造方法,必须放在第一行
2.super.fun() 调用父类方法
3.super.data 调用父类数据
参考代码:
//定义动物
class Animal{
public String name;
public Animal(String name){
this.name=name;
}
public void eat(){
System.out.println(this.name+"eat");
}
}
//猫继承动物类
class Cat extends Animal{
public Cat(String name){
super(name); //先构造父类
}
}
//鸟继承动物类
class Bird extends Animal{
public Bird(String name) {
super(name);
}
public void fly(){ //Bird自己的方法
System.out.println(this.name+"fly");
}
}
public class Test{
public static void main(String[] args) {
Cat cat=new Cat("花花");
cat.eat();
Bird bird=new Bird("小小");
bird.fly();
}
}
//打印结果
花花eat
小小fly
若将父类中的name访问权限改为private,那么子类就不能访问了
三.访问权限
在上述代码中,如果将父类的字段设置为private,那么子类就不能访问了,但是之前的public又违背了“封装”的初衷。那么此时protected关键字就很完美的解决了这个问题。
①对于类的调用者来说,protected修饰的字段和方法是不能被访问的
②对于类的子类和同一个包的其他类来说,protected修饰的字段和方法是可以访问的
Java中对字段和方法共有四种访问权限如下图:
四.复杂的继承
此时使用继承方式来表示,就会涉及到更加复杂的体系
参考代码:
class Animal{
......
}
class Cat extends Animal{
....
}
class ChineseGardenCat extends Cat{
......
}
class Orange extends ChineseGardenCat{
.....
}
....
上述的继承方式称为多层继承,即子类还可以进一步的再派生出新的子类,但是一般不希望类之间的继承层次太复杂,一般不希望出现超过三层的继承关系。如果继承关系太复杂,就要考虑对代码进行重构了。
但如果想从语法上进行限制继承,就可以使用final关键字
final关键字用法(不作深究)
①修饰类--------密封类(不能被继承)
②修饰属性-----变为常量(只读)
用final修饰的类被继承的时候,就会编译报错。
具体的final用法可以看:
final用法
五.继承中的代码块
在继承关系中,代码块的执行顺序是什么呢?
观察代码:
//动物类
class Animal{
public String name;
//静态代码块
static {
System.out.println("Animal static");
}
//实例代码块
{
System.out.println("Animal instantiation");
}
public Animal(String name){
this.name=name;
}
public void eat(){
System.out.println(this.name+"eat");
}
}
//鸟类
class Bird extends Animal{
//静态代码块
static {
System.out.println("Bird static");
}
//实例代码块
{
System.out.println("Bird instantiation");
}
public Bird(String name) {
super(name);
}
public void fly(){
System.out.println(this.name+"fly");
}
}
public class Test{
public static void main(String[] args) {
Bird bird=new Bird("小小");
bird.eat();
bird.fly();
}
}
//打印结果
Animal static
Bird static
Animal instantiation
Bird instantiation
小小eat
小小fly
打印顺序:父类静态代码块》子类静态代码块》父类实例代码块》子类实例代码块》方法
要注意静态代码块只初始化一次