mirror of
https://gitee.com/gaohongy/Java-Book.git
synced 2026-07-16 09:53:50 +00:00
修改错误
This commit is contained in:
+16
-21
@@ -303,31 +303,26 @@ super关键字调用父类构造方法还可以理解,为什么还需要super
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
public class Faculty extends Employee {
|
public class Faculty extends Employee {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Faculty();
|
new Faculty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Faculty() {
|
public Faculty() {
|
||||||
super();
|
|
||||||
System.out.println("(4) Faculty's no-arg constructor is invoked");
|
System.out.println("(4) Faculty's no-arg constructor is invoked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Employee extends PPP {
|
class Employee extends Person {
|
||||||
public Employee() {
|
public Employee() {
|
||||||
this("(2) Invoke Employees overloaded constructor");
|
this("(2) Invoke Employee’s overloaded constructor");
|
||||||
System.out.println("(3) Employee's no-arg constructor is invoked");
|
System.out.println("(3) Employee's no-arg constructor is invoked");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Employee(String s) {
|
public Employee(String s) {
|
||||||
super();
|
|
||||||
System.out.println(s);
|
System.out.println(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PPP {
|
class Person {
|
||||||
public PPP() {
|
public Person() {
|
||||||
System.out.println("(1) Person's no-arg constructor is invoked");
|
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
|
(4) Faculty's no-arg constructor is invoked
|
||||||
```
|
```
|
||||||
|
|
||||||
也就是说:PPP的构造函数被最先执行完成(注意不是最先被调用),然后是 Employee 的构造函数被执行完成,最后是 Faculty 的构造函数被执行完成。
|
也就是说:Person的构造函数被最先执行完成(注意不是最先被调用),然后是 Employee 的构造函数被执行完成,最后是 Faculty 的构造函数被执行完成。
|
||||||
|
|
||||||
```mermaid
|
```mermaid
|
||||||
sequenceDiagram
|
sequenceDiagram
|
||||||
activate Faculty()
|
activate Faculty()
|
||||||
Faculty()->>Employee(): 显式调用super()
|
Faculty()->>Employee(): 隐式调用super()
|
||||||
deactivate Faculty()
|
deactivate Faculty()
|
||||||
activate Employee()
|
activate Employee()
|
||||||
Employee()->>PPP(): 隐式调用 PPP()
|
Employee()->>Person(): 隐式调用 Person()
|
||||||
deactivate Employee()
|
deactivate Employee()
|
||||||
activate PPP()
|
activate Person()
|
||||||
Note over PPP():1
|
Note over Person():1
|
||||||
PPP()->>Employee(): PPP()调用返回
|
Person()->>Employee(): Person()调用返回
|
||||||
activate Employee()
|
activate Employee()
|
||||||
deactivate PPP()
|
deactivate Person()
|
||||||
Note over Employee():2、3
|
Note over Employee():2、3
|
||||||
deactivate Employee()
|
deactivate Employee()
|
||||||
Employee()->>Faculty():Employee()调用返回
|
Employee()->>Faculty():Employee()调用返回
|
||||||
@@ -370,7 +365,7 @@ main函数开起来比较奇怪,为什么在Faculty内中,又使用new 方
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
可以看到,这是典型的函数调用栈的方式:最先进入的函数最后退出。其实PPP类还隐式调用了Object(),会在Object小节讲解。
|
可以看到,这是典型的函数调用栈的方式:最先进入的函数最后退出。其实Person类还隐式调用了Object(),会在Object小节讲解。
|
||||||
|
|
||||||
## 3. Overriding 覆盖父类的方法
|
## 3. Overriding 覆盖父类的方法
|
||||||
|
|
||||||
@@ -482,7 +477,7 @@ This is fruit.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
覆盖(Overriding):只在父类型函数和字类型函数一样的情况下出现;
|
覆盖(Overriding):只在父类型函数和子类型函数一样的情况下出现;
|
||||||
|
|
||||||
重载(Overloading):函数签名不一样
|
重载(Overloading):函数签名不一样
|
||||||
|
|
||||||
@@ -529,11 +524,11 @@ public class Test {
|
|||||||
|
|
||||||
> Polymorphism means that a variable of a supertype can refer to a subtype object.
|
> 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.
|
> 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. 简单的例子
|
### 5.1. 简单的例子
|
||||||
|
|
||||||
@@ -598,7 +593,7 @@ public class Test {
|
|||||||
|
|
||||||
但是为什么输入结果不是`This is fruit`?这时因为子类型覆盖了父类型的`toString()`方法;真正的行为表现形式还要看对象的确切类型是什么。例子中,对象是 Apple 或者是 Orange;toString()的行为也只能是Apple或者Orange对应这个函数的真是行为,而不是他父类的行为(父类的这个行为被覆盖了)。关于这个问题,在下面的动态函数绑定中还有解释。
|
但是为什么输入结果不是`This is fruit`?这时因为子类型覆盖了父类型的`toString()`方法;真正的行为表现形式还要看对象的确切类型是什么。例子中,对象是 Apple 或者是 Orange;toString()的行为也只能是Apple或者Orange对应这个函数的真是行为,而不是他父类的行为(父类的这个行为被覆盖了)。关于这个问题,在下面的动态函数绑定中还有解释。
|
||||||
|
|
||||||
用父类型的引用变量引用字类型的对象,并调用相应的函数体现不同的行为,这就是多态。多态应用中,子类型一般需要覆盖父类型的方法。
|
用父类型的引用变量引用子类型的对象,并调用相应的函数体现不同的行为,这就是多态。多态应用中,子类型一般需要覆盖父类型的方法。
|
||||||
|
|
||||||
|
|
||||||
### 5.2. 参数中使用多态
|
### 5.2. 参数中使用多态
|
||||||
|
|||||||
Reference in New Issue
Block a user