File tree Expand file tree Collapse file tree 2 files changed +52
-6
lines changed
java-core/src/java1/core/oop/polymorphic Expand file tree Collapse file tree 2 files changed +52
-6
lines changed Original file line number Diff line number Diff line change
1
+ package java1 .core .oop .polymorphic ;
2
+
3
+ /**
4
+ * <p></p>
5
+ *
6
+ * @author jwzhao
7
+ * @version 1.0
8
+ * @date 2019/3/22 11:08
9
+ */
10
+ public class Animal {
11
+
12
+ public void eat (){
13
+ System .out .println ("Animal eating...." );
14
+ }
15
+
16
+
17
+ public void run (){
18
+ System .out .println ("我会跑" );
19
+ }
20
+
21
+ public void eat (Dog dog ){
22
+ System .out .println ("dog eating" );
23
+ }
24
+
25
+ public static void main (String [] args ) {
26
+ Animal animal = new Dog ();
27
+ animal .eat (new Dog ());
28
+ animal .run ();
29
+ }
30
+ }
31
+
32
+ class Cat extends Animal {
33
+ @ Override
34
+ public void eat (){
35
+ System .out .println ("cat eating" );
36
+ }
37
+ }
38
+
39
+ class Dog extends Animal {
40
+ @ Override
41
+ public void eat (Dog dog ){
42
+ System .out .println ("够改不了吃屎" );
43
+ }
44
+
45
+ public void run (String str ){
46
+ System .out .println ("跑到哪里" );
47
+ }
48
+ }
Original file line number Diff line number Diff line change @@ -21,8 +21,9 @@ public static void main(String[] args) {
21
21
System .out .println ("1--" + a1 .show (b ));//B继承了A,相当于传进去的是A, A and A 第三级。。。
22
22
System .out .println ("2--" + a1 .show (c ));//C继承了B。B继承了A A and A 第三级。。
23
23
System .out .println ("3--" + a1 .show (d )); /// A and D 第一级。。
24
-
25
- System .out .println ("4--" + a2 .show (b ));//引用是A,所以this代表A对象。 B and A
24
+
25
+ //引用是A,所以this代表A对象。 B and A
26
+ System .out .println ("4--" + a2 .show (b ));
26
27
System .out .println ("5--" + a2 .show (c ));// B and A 在第三级时,确定了要调用A中的show(A obj)的方法,但是,由于动态连接的问题,最终却调用了子类重写的方法
27
28
System .out .println ("6--" + a2 .show (d ));// A and D
28
29
@@ -47,13 +48,10 @@ public String show(B obj) {
47
48
return ("B and B" );
48
49
}
49
50
51
+ @ Override
50
52
public String show (A obj ) {
51
53
return ("B and A" );
52
54
}
53
-
54
- /* public String show(D obj) {
55
- return ("B and D");
56
- }*/
57
55
}
58
56
59
57
class C extends B {
You can’t perform that action at this time.
0 commit comments