修改错误

master
高宏宇 5 months ago
parent 8451eecc09
commit 4994827ebb

@ -303,31 +303,26 @@ super关键字调用父类构造方法还可以理解为什么还需要super
```java
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
super();
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends PPP {
class Employee extends Person {
public Employee() {
this("(2) Invoke Employees overloaded constructor");
this("(2) Invoke Employees overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
super();
System.out.println(s);
}
}
class PPP {
public PPP() {
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
@ -346,21 +341,21 @@ main函数开起来比较奇怪为什么在Faculty内中又使用new 方
(4) Faculty's no-arg constructor is invoked
```
也就是说PPP的构造函数被最先执行完成(注意不是最先被调用),然后是 Employee 的构造函数被执行完成,最后是 Faculty 的构造函数被执行完成。
也就是说Person的构造函数被最先执行完成(注意不是最先被调用),然后是 Employee 的构造函数被执行完成,最后是 Faculty 的构造函数被执行完成。
```mermaid
sequenceDiagram
activate Faculty()
Faculty()->>Employee(): 式调用super()
Faculty()->>Employee(): 式调用super()
deactivate Faculty()
activate Employee()
Employee()->>PPP(): 隐式调用 PPP()
Employee()->>Person(): 隐式调用 Person()
deactivate Employee()
activate PPP()
Note over PPP():1
PPP()->>Employee(): PPP()调用返回
activate Person()
Note over Person():1
Person()->>Employee(): Person()调用返回
activate Employee()
deactivate PPP()
deactivate Person()
Note over Employee():2、3
deactivate Employee()
Employee()->>Faculty():Employee()调用返回
@ -370,7 +365,7 @@ main函数开起来比较奇怪为什么在Faculty内中又使用new 方
```
可以看到这是典型的函数调用栈的方式最先进入的函数最后退出。其实PPP类还隐式调用了Object()会在Object小节讲解。
可以看到这是典型的函数调用栈的方式最先进入的函数最后退出。其实Person类还隐式调用了Object()会在Object小节讲解。
## 3. Overriding 覆盖父类的方法
@ -482,7 +477,7 @@ This is fruit.
![image-20230312150607935](img/image-20230312150607935.png)
覆盖Overriding只在父类型函数和类型函数一样的情况下出现;
覆盖Overriding只在父类型函数和类型函数一样的情况下出现;
重载Overloading函数签名不一样
@ -529,11 +524,11 @@ public class Test {
> Polymorphism means that a variable of a supertype can refer to a subtype object.
>
> 多态是指直接或者间接的父类型引用变量可以用来引用类型的对象。很难理解,有什么用?
> 多态是指直接或者间接的父类型引用变量可以用来引用类型的对象。很难理解,有什么用?
>
> A class defines a type. A type defined by a subclass is called a subtype, and a type defined by its superclass is called a supertype. Therefore, you can say that Circle is a subtype of GeometricObject and GeometricObject is a supertype for Circle.
>
> 上面是说父类型和类型的关系。例如借用本章开头的分类图:猫种可以说成是属于猫属、猫科、食肉动物目。。。就是这意思,这有什么用?
> 上面是说父类型和类型的关系。例如借用本章开头的分类图:猫种可以说成是属于猫属、猫科、食肉动物目。。。就是这意思,这有什么用?
### 5.1. 简单的例子
@ -598,7 +593,7 @@ public class Test {
但是为什么输入结果不是`This is fruit`?这时因为子类型覆盖了父类型的`toString()`方法;真正的行为表现形式还要看对象的确切类型是什么。例子中,对象是 Apple 或者是 OrangetoString()的行为也只能是Apple或者Orange对应这个函数的真是行为而不是他父类的行为父类的这个行为被覆盖了。关于这个问题在下面的动态函数绑定中还有解释。
用父类型的引用变量引用类型的对象,并调用相应的函数体现不同的行为,这就是多态。多态应用中,子类型一般需要覆盖父类型的方法。
用父类型的引用变量引用类型的对象,并调用相应的函数体现不同的行为,这就是多态。多态应用中,子类型一般需要覆盖父类型的方法。
### 5.2. 参数中使用多态

Loading…
Cancel
Save