Skip to content

Commit ef5317f

Browse files
面向对象,多态
1 parent b7986d0 commit ef5317f

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

java-core/src/java1/core/oop/polymorphic/Polymorphic.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public static void main(String[] args) {
2121
System.out.println("1--" + a1.show(b));//B继承了A,相当于传进去的是A, A and A 第三级。。。
2222
System.out.println("2--" + a1.show(c));//C继承了B。B继承了A A and A 第三级。。
2323
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));
2627
System.out.println("5--" + a2.show(c));// B and A 在第三级时,确定了要调用A中的show(A obj)的方法,但是,由于动态连接的问题,最终却调用了子类重写的方法
2728
System.out.println("6--" + a2.show(d));// A and D
2829

@@ -47,13 +48,10 @@ public String show(B obj) {
4748
return ("B and B");
4849
}
4950

51+
@Override
5052
public String show(A obj) {
5153
return ("B and A");
5254
}
53-
54-
/* public String show(D obj) {
55-
return ("B and D");
56-
}*/
5755
}
5856

5957
class C extends B {

0 commit comments

Comments
 (0)