Skip to content

Commit 2faca5e

Browse files
committed
init
1 parent e886777 commit 2faca5e

File tree

5 files changed

+253
-0
lines changed

5 files changed

+253
-0
lines changed

.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Java_Polymorphic</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
## 摘要:
2+
将实例化对象的引用赋值给超类变量,超类变量引用的是被子类重写的方法
3+
4+
否则,按照继承关系的优先级调用方法:
5+
6+
```
7+
this.method(o)<----super.method(o)<----this.method((super)o)<----super.method((super)o)
8+
```
9+
10+
11+
## 例子演示多态的特性
12+
13+
```java
14+
public class PolymorphicTest {
15+
16+
/**
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
// TODO Auto-generated method stub
21+
A a1 = new A();
22+
A a2 = new B();
23+
B b = new B();
24+
C c = new C();
25+
D d = new D();
26+
27+
System.out.println("1--" + a1.show(b));// 打印:1--A and A
28+
System.out.println("2--" + a1.show(c));// 打印:2--A and A
29+
System.out.println("3--" + a1.show(d));// 打印:3--A and D
30+
31+
System.out.println("4--" + a2.show(b));// 打印:4--B and A
32+
System.out.println("5--" + a2.show(c));// 打印:5--B and A
33+
System.out.println("6--" + a2.show(d));// 打印:6--A and D
34+
35+
System.out.println("7--" + b.show(b));// 打印:7--B and B
36+
System.out.println("8--" + b.show(c));// 打印:8--B and B
37+
System.out.println("9--" + b.show(d));// 打印:9--A and D
38+
}
39+
40+
static class A {
41+
public String show(D obj) {
42+
return ("A and D");
43+
}
44+
45+
public String show(A obj) {
46+
return ("A and A");
47+
}
48+
}
49+
50+
static class B extends A {
51+
public String show(B obj) {
52+
return ("B and B");
53+
}
54+
55+
public String show(A obj) {
56+
return ("B and A");
57+
}
58+
}
59+
60+
static class C extends B {
61+
62+
}
63+
64+
static class D extends B {
65+
66+
}
67+
68+
}
69+
```
70+
71+
![Java多态的学习](img/20180322144749.jpg)
72+
73+
74+
- 分析`A a1 = new A()`
75+
76+
`a1.show(b)`
77+
78+
先在A类中查找是否存在`show(b)`方法,即this.show(b),不存在
79+
80+
然后在A类的超类中查找是否存在`show(b)`,A的超类是Object,不存在
81+
82+
再将传入参数上溯造型查找是否存在,参数b的超类是A,即`this.show((A)b)`,发现了`show(a)`方法
83+
84+
因为a1持有的是A类的引用,所以,最终打印:`1--A and A`
85+
86+
87+
`a1.show(c)`
88+
89+
先在A类中查找是否存在`show(c)`方法,不存在
90+
91+
然后在A类的超类中查找是否存在`show(c)`,A的超类是Object,不存在
92+
93+
再将传入参数上溯造型查找是否存在,即`this.show((B)c)`,不存在
94+
95+
继续将传入参数上溯造型查找是否存在,即`this.show((A)c)`,发现了`show(a)`方法
96+
97+
因为a1持有的是A类的引用,所以,最终打印:`2--A and A`
98+
99+
*Note that:上溯造型的对象依次是当前对象的超类、祖先类*
100+
101+
102+
`a1.show(d)`
103+
104+
先在A类中查找是否存在`show(d)`方法,发现了该方法
105+
106+
因为a1持有的是A类的引用,所以,最终打印:`3--A and D`
107+
108+
109+
- 分析`A a2 = new B()`
110+
111+
`a2.show(b)`
112+
113+
先在A类中查找是否存在`show(b)`方法,不存在
114+
115+
然后在A类的超类中查找是否存在`show(b)`方法,不存在
116+
117+
再将参数上溯造型查找是否存在,即`this.show((A)b)`,发现了`show(a)`方法
118+
119+
因为a2持有的是B类的引用,进一步检查子类(即B类)是否重写了`show(a)`方法,发现重写了
120+
121+
所以,最终打印:`4--B and A`
122+
123+
124+
`a2.show(c)`
125+
126+
先在A类中查找是否存在`show(c)`,不存在
127+
128+
然后在A类的超类(即Object)中查找是否存在`show(c)`方法,不存在
129+
130+
再将参数上溯造型查找是否存在(*上溯造型的对象依次是当前对象的超类、祖先类*),即`this.show((B)c)`,不存在
131+
132+
继续将参数上溯造型查找是否存在,即`this.show((A)c)`,发现了`show(a)`方法
133+
134+
因为a2持有的是B类的引用,进一步检查子类(即B类)是否重写了`show(a)`方法,发现重写了
135+
136+
所以,最终打印:`5--B and A`
137+
138+
139+
`a2.show(d)`
140+
141+
先在A类中查找是否存在`show(d)`方法,发现了该方法
142+
143+
因为a2持有的是B类的引用,检查子类(即B类)是否重写了`show(d)`方法,没有重写
144+
145+
所以,最终打印:`6--A and D`
146+
147+
148+
- 分析`B b = new B()`
149+
150+
`b.show(b)`
151+
152+
先在B类中查找是否存在`show(b)`方法,发现了`show(b)`方法
153+
154+
因为b持有的是B类的引用,所以不用考虑重写的问题,最终打印:`7--B and B`
155+
156+
157+
`b.show(c)`
158+
159+
先在B类中查找是否存在`show(c)`方法,不存在
160+
161+
然后在B类的超类(即A类)中查找是否存在该方法,即`A.show(c)`,不存在
162+
163+
再将参数上溯造型(造型的对象是B类、A类、Object类),即`show((B)c)`,发现了`show(b)`方法
164+
165+
因为b持有的是B类的引用,所以不用考虑重写的问题,最终打印:`8--B and B`
166+
167+
168+
`b.show(d)`
169+
170+
先在B类中查找是否存在`show(d)`方法,不存在
171+
172+
然后在B类的超类(即A类)中查找是否存在该方法,即`A.show(d)`,发现了该方法
173+
174+
因为b变量持有的是B类的引用,所以不用考虑重写的问题,最终打印:`9--A and D`

img/20180322144749.jpg

54.6 KB
Loading
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cn.teachcourse;
2+
3+
public class PolymorphicTest {
4+
5+
/**
6+
* @param args
7+
*/
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
A a1 = new A();
11+
A a2 = new B();
12+
B b = new B();
13+
C c = new C();
14+
D d = new D();
15+
16+
System.out.println("1--" + a1.show(b));//打印:1--A and A
17+
System.out.println("2--" + a1.show(c));//打印:2--A and A
18+
System.out.println("3--" + a1.show(d));//打印:3--A and D
19+
20+
System.out.println("4--" + a2.show(b));//打印:4--B and A
21+
System.out.println("5--" + a2.show(c));//打印:5--B and A
22+
System.out.println("6--" + a2.show(d));//打印:6--A and D
23+
24+
System.out.println("7--" + b.show(b));//打印:7--B and B
25+
System.out.println("8--" + b.show(c));//打印:8--B and B
26+
System.out.println("9--" + b.show(d));//打印:9--A and D
27+
}
28+
static class A {
29+
public String show(D obj) {
30+
return ("A and D");
31+
}
32+
33+
public String show(A obj) {
34+
return ("A and A");
35+
}
36+
}
37+
38+
static class B extends A {
39+
public String show(B obj) {
40+
return ("B and B");
41+
}
42+
43+
public String show(A obj) {
44+
return ("B and A");
45+
}
46+
}
47+
48+
static class C extends B {
49+
50+
}
51+
52+
static class D extends B {
53+
54+
}
55+
56+
}

0 commit comments

Comments
 (0)