auto commit

This commit is contained in:
CyC2018 2019-02-17 00:48:28 +08:00
parent 8e279530cc
commit f0bd74fb60

View File

@ -676,67 +676,65 @@ SuperExtendExample.func()
应该注意的是,返回值不同,其它都相同不算是重载。
**3. 重写与重载的例子**
**3. 实例**
类定义如下:
```java
class A{
public String show(D obj){
class A {
public String show(D obj) {
return ("A and D");
}
public String show(A obj){
public String show(A obj) {
return ("A and A");
}
}
class B extends A{
public String show(B obj){
class B extends A {
public String show(B obj) {
return ("B and B");
}
public String show(A obj){
public String show(A obj) {
return ("B and A");
}
}
class C extends B{}
class D extends B{}
```
main方法如下:
class C extends B {
}
class D extends B {
}
```
```java
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b));
System.out.println(a1.show(c));
System.out.println(a1.show(d));
System.out.println(a2.show(b));
System.out.println(a2.show(c));
System.out.println(a2.show(d));
System.out.println(b.show(b));
System.out.println(b.show(c));
System.out.println(b.show(d));
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println(a1.show(b)); // A and A
System.out.println(a1.show(c)); // A and A
System.out.println(a1.show(d)); // A and D
System.out.println(a2.show(b)); // B and A
System.out.println(a2.show(c)); // B and A
System.out.println(a2.show(d)); // A and D
System.out.println(b.show(b)); // B and B
System.out.println(b.show(c)); // B and B
System.out.println(b.show(d)); // A and D
}
}
```
请问输出是什么?
涉及到重写时,方法调用的优先级为:
实际上这里涉及方法调用的优先级问题 优先级由高到低依次为this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O)。
- this.show(O)
- super.show(O)
- this.show((super)O)
- super.show((super)O)
以a2.show(b)为例a2是一个引用变量类型为A则this为a2b是B的一个实例于是它到类A里面找show(B obj)方法没有找到于是到A的super(超类)找而A没有超类因此转到第三优先级this.show((super)O)this仍然是a2这里O为B(super)O即(super)B即A因此它到类A里面找show(A obj)的方法类A有这个方法但是由于a2引用的是类B的一个对象B覆盖了A的show(A obj)方法因此最终锁定到类B的show(A obj),输出为"B and A”。
所以答案分别是:
- A and A
- A and A
- A and D
- B and A
- B and A
- A and D
- B and B
- B and B
- A and D
# 五、Object 通用方法
## 概览