8000 修改语句拗口问题 by andyphone · Pull Request #626 · lingcoder/OnJava8 · GitHub
[go: up one dir, main page]

Skip to content
8000

修改语句拗口问题 #626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Dec 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
修改语句拗口问题 3
  • Loading branch information
jim.deng committed Nov 25, 2020
commit 34e5908b67f226464a98d357aaec517faa212743
13 changes: 10 additions & 3 deletions docs/book/08-Reuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,10 @@ doh(Milhouse)

````

**Homer** 的所有重载方法在 **Bart** 中都是可用的,尽管 **Bart** 引入了一种新的重载方法。正如你将在下一章中看到的那样,比起重载,更常见的是覆盖同名方法,使用与基类中完全相同的方法签名(方法名和参数类型)和返回类型。否则会让人感到困惑。
**Homer** 的所有重载方法在 **Bart** 中都是可用的,尽管 **Bart** 引入了一种新的重载方法。正如你将在下一章中看到的那样,比起重载,更常见的是覆盖同名方法,使用与基类中完全相同的方法签名[^1]和返回类型。否则会让人感到困惑。

你已经看到了Java 5 **@Override**注释,它不是关键字,但是可以像使用关键字一样使用它。当你打算重写一个方法[^2]时,你可以选择添加这个注释,如果你不小心用了重载而不是重写,编译器会产生一个错误消息:

你已经看到了Java 5 **@Override**注释,它不是关键字,但是可以像使用关键字一样使用它。当你打算重写一个方法(覆盖同名方法)时,你可以选择添加这个注释,如果你不小心用了重载而不是重写,编译器会产生一个错误消息:

```java
// reuse/Lisa.java
Expand All @@ -709,8 +710,14 @@ class Lisa extends Homer {

```

**{WillNotCompile}** 标记将该文件排除在本书的 **Gradle** 构建之外,但是如果你手工编译它,你将看到:method does not override a method from its superclass.方法不会覆盖(重写)超类中的方法, **@Override** 注释能防止你意外地重载。
**{WillNotCompile}** 标记将该文件排除在本书的 **Gradle** 构建之外,但是如果你手工编译它,你将看到:method does not override a method from its superclass.方法不会重写超类中的方法, **@Override** 注释能防止你意外地重载。

- **[1]** 方法签名——方法名和参数类型的合称

- **[2]** 重写——覆盖同名方法,使用与基类中完全相同的方法签名和返回类型[^3]

- **[3]** 在java 1.4版本以前,重写方法的返回值类型被要求必须与被重写方法一致,但是在java 5.0中放宽了这一个限制,添加了对协变返回类型的支持,在重写的时候,重写方法的返回值类型可以是被重写方法返回值类型的子类。

<!-- Choosing Composition vs. Inheritance -->

## 组合与继承的选择
Expand Down
0