1
1
package cn .teachcourse ;
2
2
3
+ /**
4
+ * Created by http://teachcourse.cn on 2018/03/22.
5
+ */
3
6
public class PolymorphicTest {
4
7
5
8
/**
@@ -13,18 +16,19 @@ public static void main(String[] args) {
13
16
C c = new C ();
14
17
D d = new D ();
15
18
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
19
+ System .out .println ("1--" + a1 .show (b ));// 打印:1--A and A
20
+ System .out .println ("2--" + a1 .show (c ));// 打印:2--A and A
21
+ System .out .println ("3--" + a1 .show (d ));// 打印:3--A and D
22
+
23
+ System .out .println ("4--" + a2 .show (b ));// 打印:4--B and A
24
+ System .out .println ("5--" + a2 .show (c ));// 打印:5--B and A
25
+ System .out .println ("6--" + a2 .show (d ));// 打印:6--A and D
26
+
27
+ System .out .println ("7--" + b .show (b ));// 打印:7--B and B
28
+ System .out .println ("8--" + b .show (c ));// 打印:8--B and B
29
+ System .out .println ("9--" + b .show (d ));// 打印:9--A and D
27
30
}
31
+
28
32
static class A {
29
33
public String show (D obj ) {
30
34
return ("A and D" );
@@ -46,11 +50,11 @@ public String show(A obj) {
46
50
}
47
51
48
52
static class C extends B {
49
-
53
+
50
54
}
51
55
52
56
static class D extends B {
53
-
57
+
54
58
}
55
59
56
60
}
0 commit comments