Open YuezhenQin opened 4 months ago
One trait would mask the other. - Gregor Mendel
为了实现继承特性,编程语言需要提供特殊的语法机制,例如 java 使用 extends
关键字实现继承。
extends
: class B extends Asuper()
: to access fields of the base class, or to invoke methods and constructors of the base classpublic abstract class A { //1.抽象类可以包含属性和方法
private int x;
private int y;
public A(int x, int y) {
this.x = x;
this.y = y;
}
...
public abstract void do(); //抽象方法
}
public class B extends A { //2.抽象类不允许被实例化,只能被继承
private int z;
public B(int x, int y, int z) {
super(x, y);
this.z = z;
}
@Override
public void do() {...} //3.编译器会要求子类必须实现抽象类(父类)中的所有抽象方法(抽象类的存在意义)
}
继承主要有 3 个作用,表示 is-a 关系,支持多态特性,代码重复利用。is-a 关系可以通过 has-a 关系替代,多态特性可以通过接口支持,代码复用可以通过委托实现。
public interface Flyable { //1. 接口
void fly();
}
public class Flyability {
@Override
public void fly() {...}
}
public interface Tweetable {
void tweet();
}
public class Tweetability implements Tweetable {
@Override
public void tweet() {...}
}
public class Sparrow implements Flyable, Tweetable {
private Flyability f= new Flyability(); // 2. 组合
@Override
public void fly() {
flyability.fly(); //3. 委托
}
}
我们知道封装、继承特性都需要编程语言提供特殊的语法机制来实现,多态特性也是如此。
封装不稳定的实现细节,暴露稳定的接口。上游系统面向下游系统提供的接口编程,不依赖不稳定的实现细节,而依赖稳定的接口。这样当下游系统实现细节发生变化的时候,上游系统的代码也基本不需要改动。
封装 Encapsulation: 信息隐藏和数据访问保护
封装特性需要编程语言提供特殊的语法机制来实现(
public
、private
关键字)。