mirror of
https://gitee.com/gaohongy/Java-Book.git
synced 2026-07-16 10:33:49 +00:00
修正全书错别字、术语统一和逻辑错误
This commit is contained in:
+6
-6
@@ -122,7 +122,7 @@ public class Circle extends GeometricObject {
|
||||
```java
|
||||
public class Rectangle extends GeometricObject {
|
||||
private double width;
|
||||
private Double height;
|
||||
private double height;
|
||||
|
||||
public Rectangle() {
|
||||
}
|
||||
@@ -164,7 +164,7 @@ public class Rectangle extends GeometricObject {
|
||||
}
|
||||
```
|
||||
|
||||
上述两个类都实现(impliment)了(覆盖了)`getArea()` 和 `getPerimeter()`两个函数(这两个函数都有函数体),且这两个类都没有使用 abstract 进行修饰,那么这两个类是可以被实例化的。
|
||||
上述两个类都实现(implement)了(覆盖了)`getArea()` 和 `getPerimeter()`两个函数(这两个函数都有函数体),且这两个类都没有使用 abstract 进行修饰,那么这两个类是可以被实例化的。
|
||||
|
||||
看看主函数如何使用多态调用的:
|
||||
|
||||
@@ -209,7 +209,7 @@ public class TestGeometricObject {
|
||||
|
||||
1. 实例化我没有任何实际的意义;
|
||||
2. 我只是描述我后代必须具备的能力;
|
||||
3. 后代的某些能力我知道是什么(函数原型),但是我不知道如何实现(算法),因为他们变化太多了;这些能力我就只描述成抽象的方法(只有函数申明,没有函数体)。
|
||||
3. 后代的某些能力我知道是什么(函数原型),但是我不知道如何实现(算法),因为他们变化太多了;这些能力我就只描述成抽象的方法(只有函数声明,没有函数体)。
|
||||
4. 有些能力我知道如何实现,这些能力就是非抽象的方法。
|
||||
|
||||
**抽象类不能实例化,但是作为参数,用作多态实现通用编程非常有用!**
|
||||
@@ -393,7 +393,7 @@ public interface Edible {
|
||||
|
||||
> An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class. Like an abstract class, you cannot create an instance from an interface using the new operator, but in most cases you can use an interface more or less the same way you use an abstract class. For example, you can use an interface as a data type for a variable, as the result of casting, and so on.
|
||||
>
|
||||
> 接口如同一个特殊的类(class),每个接口被编译成一个对立的字节代码(bytecode)...
|
||||
> 接口如同一个特殊的类(class),每个接口被编译成一个独立的字节代码(bytecode)...
|
||||
>
|
||||
> 接口不能被实例化(使用new关键字),但是可以作为引用变量来引用实现了该接口的类的对象;这种情况下,接口的使用如同抽象类。
|
||||
|
||||
@@ -408,7 +408,7 @@ public interface Edible {
|
||||
1. 定义一个接口Edible(可食用的),其中包含一个抽象函数,返回如何烹饪的字符串描述;显然作为可食用这种能力(特性),在大自然中并不是某个类属所拥有的,而是跨越类属的一般特性;某些植物可以食用,某些动物可以食用;但不是所有的动物或者植物都可以食用。
|
||||
2. 作为动物(这里应该是脯乳动物),可以发声是脯乳动物的的共性,但是发出什么样的声音确是每个种类都不一样。因此定义了一个抽象相类Animal,有个抽象方法:sound。
|
||||
|
||||
上图中,虚线箭头表示接口实现,实线表示类继承(扩展)关系。上图中可以看到,水果(Fruit)都是可以食用的,因此水果这个抽象类(斜体表示)实现了可食用的接口;Chicken和Tiger都可以发声,因此由Animal这个抽象类扩展而来,且实现了发生的这个抽象方法;动物中只有Chicken可以食用,因此Chicken还实现了可食用的接口。
|
||||
上图中,虚线箭头表示接口实现,实线表示类继承(扩展)关系。上图中可以看到,水果(Fruit)都是可以食用的,因此水果这个抽象类(斜体表示)实现了可食用的接口;Chicken和Tiger都可以发声,因此由Animal这个抽象类扩展而来,且实现了发声的这个抽象方法;动物中只有Chicken可以食用,因此Chicken还实现了可食用的接口。
|
||||
|
||||
在代码中,先看看抽象类Animal:
|
||||
|
||||
@@ -469,7 +469,7 @@ class Tiger extends Animal {
|
||||
}
|
||||
```
|
||||
|
||||
Chicken很特殊,既可以发声,又能食用,因此由Anamal扩展而来,且实现Edible的接口:
|
||||
Chicken很特殊,既可以发声,又能食用,因此由Animal扩展而来,且实现Edible的接口:
|
||||
|
||||
```java
|
||||
class Chicken extends Animal implements Edible {
|
||||
|
||||
Reference in New Issue
Block a user