auto commit
This commit is contained in:
@ -37,6 +37,7 @@
|
||||
|
||||
```java
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
private int gender;
|
||||
private int age;
|
||||
@ -63,17 +64,20 @@ public class Person {
|
||||
|
||||
继承实现了 **IS-A** 关系,例如 Cat 和 Animal 就是一种 IS-A 关系,因此 Cat 可以继承自 Animal,从而获得 Animal 非 private 的属性和方法。
|
||||
|
||||
继承应该遵循里氏替换原则,子类对象必须能够替换掉所有父类对象。
|
||||
|
||||
Cat 可以当做 Animal 来使用,也就是说可以使用 Animal 引用 Cat 对象。父类引用指向子类对象称为 **向上转型** 。
|
||||
|
||||
```java
|
||||
Animal animal = new Cat();
|
||||
```
|
||||
|
||||
继承应该遵循里氏替换原则,子类对象必须能够替换掉所有父类对象。
|
||||
|
||||
## 多态
|
||||
|
||||
多态分为编译时多态和运行时多态。编译时多态主要指方法的重载,运行时多态指程序中定义的对象引用所指向的具体类型在运行期间才确定。
|
||||
多态分为编译时多态和运行时多态:
|
||||
|
||||
- 编译时多态主要指方法的重载
|
||||
- 运行时多态指程序中定义的对象引用所指向的具体类型在运行期间才确定
|
||||
|
||||
运行时多态有三个条件:
|
||||
|
||||
@ -116,7 +120,7 @@ public class Music {
|
||||
|
||||
# 二、类图
|
||||
|
||||
以下类图使用 [PlantUML](https://www.planttext.com/) 绘制,更多语法及使用请参考:http://plantuml.com/
|
||||
以下类图使用 [PlantUML](https://www.planttext.com/) 绘制,更多语法及使用请参考:http://plantuml.com/ 。
|
||||
|
||||
## 泛化关系 (Generalization)
|
||||
|
||||
@ -327,7 +331,7 @@ Vihicle .. N
|
||||
|
||||
### 2. 合成复用原则
|
||||
|
||||
尽量使用对象组合,而不是继承来达到复用的目的。
|
||||
尽量使用对象组合,而不是通过继承来达到复用的目的。
|
||||
|
||||
### 3. 共同封闭原则
|
||||
|
||||
@ -349,3 +353,4 @@ Vihicle .. N
|
||||
- [看懂 UML 类图和时序图](http://design-patterns.readthedocs.io/zh_CN/latest/read_uml.html#generalization)
|
||||
- [UML 系列——时序图(顺序图)sequence diagram](http://www.cnblogs.com/wolf-sun/p/UML-Sequence-diagram.html)
|
||||
- [面向对象编程三大特性 ------ 封装、继承、多态](http://blog.csdn.net/jianyuerensheng/article/details/51602015)
|
||||
|
||||
|
Reference in New Issue
Block a user