auto commit
This commit is contained in:
parent
9721b0233c
commit
a36bccc3ba
@ -467,7 +467,6 @@ public class AccessExample {
|
|||||||
|
|
||||||
可以使用公有的 getter 和 setter 方法来替换公有字段,这样的话就可以控制对字段的修改行为。
|
可以使用公有的 getter 和 setter 方法来替换公有字段,这样的话就可以控制对字段的修改行为。
|
||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class AccessExample {
|
public class AccessExample {
|
||||||
|
|
||||||
@ -670,7 +669,11 @@ SuperExtendExample.func()
|
|||||||
|
|
||||||
存在于继承体系中,指子类实现了一个与父类在方法声明上完全相同的一个方法。
|
存在于继承体系中,指子类实现了一个与父类在方法声明上完全相同的一个方法。
|
||||||
|
|
||||||
为了满足里式替换原则,重写有有以下两个限制:
|
为了满足里式替换原则,重写有以下三个限制:
|
||||||
|
|
||||||
|
- 子类方法的访问权限必须大于等于父类方法;
|
||||||
|
- 子类方法的返回类型必须是父类方法返回类型或为其子类型。
|
||||||
|
- 子类方法抛出的异常类型必须是父类抛出异常类型或为其子类型。
|
||||||
|
|
||||||
使用 @Override 注解,可以让编译器帮忙检查是否满足上面的三个限制条件。
|
使用 @Override 注解,可以让编译器帮忙检查是否满足上面的三个限制条件。
|
||||||
|
|
||||||
@ -696,70 +699,81 @@ class SubClass extends SuperClass {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**2. 重载(Overload)**
|
在调用一个方法时,先从本类中查找看是否有对应的方法,如果没有查找到再到父类中查看,看是否有继承来的方法。否则就要对参数进行转型,转成父类之后看是否有对应的方法。总的来说,方法调用的优先级为:
|
||||||
|
|
||||||
存在于同一个类中,指一个方法与已经存在的方法名称上相同,但是参数类型、个数、顺序至少有一个不同。
|
- this.func(this)
|
||||||
|
- super.func(this)
|
||||||
|
- this.func(super)
|
||||||
|
- super.func(super)
|
||||||
|
|
||||||
应该注意的是,返回值不同,其它都相同不算是重载。
|
|
||||||
|
|
||||||
**3. 实例**
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
/*
|
||||||
|
A
|
||||||
|
|
|
||||||
|
B
|
||||||
|
|
|
||||||
|
C
|
||||||
|
|
|
||||||
|
D
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
class A {
|
class A {
|
||||||
public String show(D obj) {
|
|
||||||
return ("A and D");
|
public void show(A obj) {
|
||||||
|
System.out.println("A.show(A)");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String show(A obj) {
|
public void show(C obj) {
|
||||||
return ("A and A");
|
System.out.println("A.show(C)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class B extends A {
|
class B extends A {
|
||||||
public String show(B obj) {
|
|
||||||
return ("B and B");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String show(A obj) {
|
@Override
|
||||||
return ("B and A");
|
public void show(A obj) {
|
||||||
|
System.out.println("B.show(A)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class C extends B {
|
class C extends B {
|
||||||
}
|
}
|
||||||
|
|
||||||
class D extends B {
|
class D extends C {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class Test {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
A a = new A();
|
||||||
A a1 = new A();
|
B b = new B();
|
||||||
A a2 = new B();
|
C c = new C();
|
||||||
B b = new B();
|
D d = new D();
|
||||||
C c = new C();
|
|
||||||
D d = new D();
|
// 在 A 中存在 show(A obj),直接调用
|
||||||
System.out.println(a1.show(b)); // A and A
|
a.show(a); // A.show(A)
|
||||||
System.out.println(a1.show(c)); // A and A
|
// 在 A 中不存在 show(B obj),将 B 转型成其父类 A
|
||||||
System.out.println(a1.show(d)); // A and D
|
a.show(b); // A.show(A)
|
||||||
System.out.println(a2.show(b)); // B and A
|
// 在 B 中存在从 A 继承来的 show(C obj),直接调用
|
||||||
System.out.println(a2.show(c)); // B and A
|
b.show(c); // A.show(C)
|
||||||
System.out.println(a2.show(d)); // A and D
|
// 在 B 中不存在 show(D obj),但是存在从 A 继承来的 show(C obj),将 D 转型成其父类 C
|
||||||
System.out.println(b.show(b)); // B and B
|
b.show(d); // A.show(C)
|
||||||
System.out.println(b.show(c)); // B and B
|
|
||||||
System.out.println(b.show(d)); // A and D
|
// 引用的还是 B 对象,所以 ba 和 b 的调用结果一样
|
||||||
}
|
A ba = new B();
|
||||||
|
ba.show(c); // A.show(C)
|
||||||
|
ba.show(d); // A.show(C)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
涉及到重写时,方法调用的优先级为:
|
**2. 重载(Overload)**
|
||||||
|
|
||||||
- this.show(O)
|
存在于同一个类中,指一个方法与已经存在的方法名称上相同,但是参数类型、个数、顺序至少有一个不同。
|
||||||
- super.show(O)
|
|
||||||
- this.show((super)O)
|
应该注意的是,返回值不同,其它都相同不算是重载。
|
||||||
- super.show((super)O)
|
|
||||||
|
|
||||||
# 五、Object 通用方法
|
# 五、Object 通用方法
|
||||||
|
|
||||||
|
@ -467,7 +467,6 @@ public class AccessExample {
|
|||||||
|
|
||||||
可以使用公有的 getter 和 setter 方法来替换公有字段,这样的话就可以控制对字段的修改行为。
|
可以使用公有的 getter 和 setter 方法来替换公有字段,这样的话就可以控制对字段的修改行为。
|
||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class AccessExample {
|
public class AccessExample {
|
||||||
|
|
||||||
@ -670,7 +669,11 @@ SuperExtendExample.func()
|
|||||||
|
|
||||||
存在于继承体系中,指子类实现了一个与父类在方法声明上完全相同的一个方法。
|
存在于继承体系中,指子类实现了一个与父类在方法声明上完全相同的一个方法。
|
||||||
|
|
||||||
为了满足里式替换原则,重写有有以下两个限制:
|
为了满足里式替换原则,重写有以下三个限制:
|
||||||
|
|
||||||
|
- 子类方法的访问权限必须大于等于父类方法;
|
||||||
|
- 子类方法的返回类型必须是父类方法返回类型或为其子类型。
|
||||||
|
- 子类方法抛出的异常类型必须是父类抛出异常类型或为其子类型。
|
||||||
|
|
||||||
使用 @Override 注解,可以让编译器帮忙检查是否满足上面的三个限制条件。
|
使用 @Override 注解,可以让编译器帮忙检查是否满足上面的三个限制条件。
|
||||||
|
|
||||||
@ -696,70 +699,81 @@ class SubClass extends SuperClass {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**2. 重载(Overload)**
|
在调用一个方法时,先从本类中查找看是否有对应的方法,如果没有查找到再到父类中查看,看是否有继承来的方法。否则就要对参数进行转型,转成父类之后看是否有对应的方法。总的来说,方法调用的优先级为:
|
||||||
|
|
||||||
存在于同一个类中,指一个方法与已经存在的方法名称上相同,但是参数类型、个数、顺序至少有一个不同。
|
- this.func(this)
|
||||||
|
- super.func(this)
|
||||||
|
- this.func(super)
|
||||||
|
- super.func(super)
|
||||||
|
|
||||||
应该注意的是,返回值不同,其它都相同不算是重载。
|
|
||||||
|
|
||||||
**3. 实例**
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
/*
|
||||||
|
A
|
||||||
|
|
|
||||||
|
B
|
||||||
|
|
|
||||||
|
C
|
||||||
|
|
|
||||||
|
D
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
class A {
|
class A {
|
||||||
public String show(D obj) {
|
|
||||||
return ("A and D");
|
public void show(A obj) {
|
||||||
|
System.out.println("A.show(A)");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String show(A obj) {
|
public void show(C obj) {
|
||||||
return ("A and A");
|
System.out.println("A.show(C)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class B extends A {
|
class B extends A {
|
||||||
public String show(B obj) {
|
|
||||||
return ("B and B");
|
|
||||||
}
|
|
||||||
|
|
||||||
public String show(A obj) {
|
@Override
|
||||||
return ("B and A");
|
public void show(A obj) {
|
||||||
|
System.out.println("B.show(A)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class C extends B {
|
class C extends B {
|
||||||
}
|
}
|
||||||
|
|
||||||
class D extends B {
|
class D extends C {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class Test {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
A a = new A();
|
||||||
A a1 = new A();
|
B b = new B();
|
||||||
A a2 = new B();
|
C c = new C();
|
||||||
B b = new B();
|
D d = new D();
|
||||||
C c = new C();
|
|
||||||
D d = new D();
|
// 在 A 中存在 show(A obj),直接调用
|
||||||
System.out.println(a1.show(b)); // A and A
|
a.show(a); // A.show(A)
|
||||||
System.out.println(a1.show(c)); // A and A
|
// 在 A 中不存在 show(B obj),将 B 转型成其父类 A
|
||||||
System.out.println(a1.show(d)); // A and D
|
a.show(b); // A.show(A)
|
||||||
System.out.println(a2.show(b)); // B and A
|
// 在 B 中存在从 A 继承来的 show(C obj),直接调用
|
||||||
System.out.println(a2.show(c)); // B and A
|
b.show(c); // A.show(C)
|
||||||
System.out.println(a2.show(d)); // A and D
|
// 在 B 中不存在 show(D obj),但是存在从 A 继承来的 show(C obj),将 D 转型成其父类 C
|
||||||
System.out.println(b.show(b)); // B and B
|
b.show(d); // A.show(C)
|
||||||
System.out.println(b.show(c)); // B and B
|
|
||||||
System.out.println(b.show(d)); // A and D
|
// 引用的还是 B 对象,所以 ba 和 b 的调用结果一样
|
||||||
}
|
A ba = new B();
|
||||||
|
ba.show(c); // A.show(C)
|
||||||
|
ba.show(d); // A.show(C)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
涉及到重写时,方法调用的优先级为:
|
**2. 重载(Overload)**
|
||||||
|
|
||||||
- this.show(O)
|
存在于同一个类中,指一个方法与已经存在的方法名称上相同,但是参数类型、个数、顺序至少有一个不同。
|
||||||
- super.show(O)
|
|
||||||
- this.show((super)O)
|
应该注意的是,返回值不同,其它都相同不算是重载。
|
||||||
- super.show((super)O)
|
|
||||||
|
|
||||||
# 五、Object 通用方法
|
# 五、Object 通用方法
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user