面向对象编程 -------继承

为什么引入继承

代码中创建的类, 主要是为了抽象现实中的一些事物(包含属性和方法).
有的时候客观事物之间就存在一些关联关系, 那么在表示成类和对象的时候也会存在一定的关联.
例如, 设计一个类表示动物

#注意:我们可以给每个类创建一个单独的 java 文件. 类名必须和 .java 文件名匹配(大小写敏感).
// Animal.java
public class Animal {
  public String name;
 
  public Animal(String name) {
 this.name = name;
 }
 
  public void eat(String food) {
    System.out.println(this.name + "正在吃" + food);
 }
}
// Cat.java
class Cat {
public String name;
 
  public Cat(String name) {
 this.name = name;
 }
 
  public void eat(String food) {
    System.out.println(this.name + "正在吃" + food);
 }
}
// Bird.java
class Bird {
public String name;
 
  public Bird(String name) {
    this.name = name;
 }
 
  public void eat(String food) {
    System.out.println(this.name + "正在吃" + food);
 }
 
  public void fly() {
    System.out.println(this.name + "正在飞 !");
 }
}

这个代码我们发现其中存在了大量的冗余代码.
仔细分析, 我们发现 Animal 和 Cat 以及 Bird 这几个类中存在一定的关联关系:
1、这三个类都具备一个相同的 eat 方法, 而且行为是完全一样的.
2、这三个类都具备一个相同的 name 属性, 而且意义是完全一样的.
3、从逻辑上讲, Cat 和 Bird 都是一种 Animal (is - a 语义).
此时我们就可以让 Cat 和 Bird 分别继承 Animal 类, 来达到代码重用的效果.并且Animal 这样被继承的类, 我们称为 父类 , 基类 或 超类, 对于像 Cat 和 Bird 这样的类, 我们称为 子类, 派生类和现实中的儿子继承父亲的财产类似, 子类也会继承父类的字段和方法(子类包含父类的方法和属性), 以达到代码重用的效果.

语法规则

1、使用 extends 指定父类.
2、Java 中一个子类只能继承一个父类 (而C++/Python等语言支持多继承).
3、子类会继承父类除了构造方法外的所有字段和方法.
4、对于父类的 private 的字段和方法, 子类中是无法访问的.
5、子类的实例中, 也包含着父类的实例. 可以使用 super 关键字得到父类实例的引用
6、如果子类要实例化那么首先要实例化父类;如果子类要构造,那么要首先构造父类。

class Animal {
  public String name;
  public Animal(String name) {
    this.name = name;
 }
  public void eat(String food) {
    System.out.println(this.name + "正在吃" + food);
 }
}
class Cat extends Animal {
  public Cat(String name) {
    // 使用 super 调用父类的构造方法.
    super(name);
 }
}
class Bird extends Animal {
  public Bird(String name) {
   super(name);
 }
  public void fly() {
    System.out.println(this.name + "正在飞 ︿( ̄︶ ̄)︿");
 }
}
public class Test {
  public static void main(String[] args) {
    Cat cat = new Cat("小7");
    cat.eat("food");
    Bird bird = new Bird("17");
    bird.fly();
 }
}

如果我们把 name 改成 private, 那么此时子类就不能访问了!!

class Bird extends Animal {
  public Bird(String name) {
    super(name);
 }
  public void fly() {
    System.out.println(this.name + "正在飞 ︿( ̄︶ ̄)︿");
 }
}
// 编译出错
//Error:(19, 32) java: name 在 Animal 中是 private 访问控制

protected 关键字

刚才我们发现, 如果把字段设为 private, 子类不能访问. 但是设成public, 又违背了我们 “封装” 的初衷.两全其美的办法就是protected 关键字.
1、对于类的调用者来说, protected 修饰的字段和方法是不能访问的
2、对于类的 子类 和 同一个包的其他类 来说, protected 修饰的字段和方法是可以访问的

// Animal.java
public class Animal {
  protected String name;
  public Animal(String name) {
  this.name = name;
 }
  public void eat(String food) {
    System.out.println(this.name + "正在吃" + food);
 }
}
// Bird.java
public class Bird extends Animal {
  public Bird(String name) {
    super(name);
 }
  public void fly() {
    // 对于父类的 protected 字段, 子类可以正确访问
    System.out.println(this.name + "正在飞 ");
 }
}
// Test.java 和 Animal.java 不在同一个 包 之中了.
public class Test {
  public static void main(String[] args) {
    Animal animal = new Animal("小动物");
    System.out.println(animal.name); // 此时编译出错, 无法访问 name
 }
}

如刚才这样的继承方式称为多层继承, 即子类还可以进一步的再派生出新的子类.

#时刻牢记, 我们写的类是现实事物的抽象. 而我们真正在公司中所遇到的项目往往业务比较复杂, 可能会涉及到一系列复杂的概念, 都需要我们使用代码来表示, 所以我们真实项目中所写的类也会有很多. 类之间的关系也会更加复杂.
 但是即使如此, 我们并不希望类之间的继承层次太复杂. 一般我们不希望出现超过三层的继承关系. 如果继承层次太多, 就需要考虑对代码进行重构了.
 如果想从语法上进行限制继承, 就可以使用 final 关键字

final 关键字

如果一个类不想被继承, 就可以使用 final 关键字
final不仅可以修饰常量,也可以修饰方法,以及类。我们把final修饰的类称作密封类,把final修饰的方法称为密封方法。

组合

和继承类似, 组合也是一种表达类之间关系的方式, 也是能够达到代码重用的效果.
例如:

public class Student {
...
}
public class Teacher {
...
}
public class School {
public Student[] students;
public Teacher[] teachers;
}
 

组合并没有涉及到特殊的语法(诸如 extends 这样的关键字), 仅仅是将一个类的实例作为另外一个类的字段.这是我们设计类的一种常用方式之一.

#组合表示  has - a 语义
 在刚才的例子中, 我们可以理解成一个学校中 "包含" 若干学生和教师.
 继承表示  is - a 语义
 在上面的 "动物和猫" 的例子中, 我们可以理解成一只猫也 "是" 一种动物
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值