Skip to content

Commit c850724

Browse files
author
xiachaochao
committed
多态01
1 parent 5ebd5af commit c850724

File tree

7 files changed

+88
-25
lines changed

7 files changed

+88
-25
lines changed

JavaBase/.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>JavaBase</name>
3+
<name>JavaBaseCrazy</name>
44
<comment></comment>
55
<projects>
66
</projects>
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
package com.xinan.testoop;
22

33
public class Dog extends Pet {
4+
public Dog() {
5+
super();
6+
}
7+
8+
public Dog(String name) {
9+
super(name);
10+
}
11+
12+
public Dog(String name, int age, int love) {
13+
super(name, age, love);
14+
}
15+
public void eat(){
16+
System.out.println("小狗吃东西");
17+
}
418
public void catchingFlyDisc(){
5-
System.out.println("");
19+
System.out.println("小狗玩飞碟");
620
}
721
}

JavaBase/src/com/xinan/testoop/LomBokDemo.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
package com.xinan.testoop;
22

33
public class Master {
4-
5-
public Pet getPet(String typeId){
6-
if(typeId.equalsIgnoreCase("dog")){
7-
Pet pet=new Dog();
8-
return pet;
9-
}
10-
else{
11-
return null;
12-
}
4+
public void play(Pet pet){//使用父类Pet pet作为方法形参实现多态
5+
if(pet instanceof Dog){
6+
Dog dog=(Dog) pet;
7+
dog.catchingFlyDisc();
8+
}
9+
else if(pet instanceof Penguin){
10+
Penguin penguin=(Penguin) pet;
11+
penguin.swimming();
12+
}
13+
else{
14+
System.out.println("您输入的有误");
15+
}
16+
}
17+
public Pet getPet(String typeId){
18+
if(typeId.equalsIgnoreCase("dog")){//equalsIgnoreCase忽略大小写
19+
Pet pet=new Dog();
20+
return pet;
21+
}
22+
else if(typeId.equalsIgnoreCase("Penguin")){
23+
Pet pet=new Penguin();
24+
return pet;
25+
}
26+
else{
27+
System.out.println("您输入的有误");
28+
return null;
1329
}
1430
}
31+
}
1532

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
package com.xinan.testoop;
22

3-
public class Penguin extends Pet{
4-
public void swimming(){
5-
System.out.println("");
3+
public class Penguin extends Pet {
4+
5+
public Penguin() {
6+
super();
7+
}
8+
9+
public Penguin(String name) {
10+
super(name);
11+
}
12+
13+
public Penguin(String name, int age, int love) {
14+
super(name, age, love);
15+
}
16+
17+
public void eat() {
18+
System.out.println("Penguin eat!");
19+
}
20+
21+
public void swimming() {
22+
System.out.println("和企鹅玩游泳!!");
623
}
7-
}
24+
}
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package com.xinan.testoop;
22

33
public abstract class Pet {
4-
4+
String name;
5+
int age=50;
6+
private int love=100;
7+
public Pet(){}
8+
public Pet(String name){
9+
super();
10+
this.name=name;
11+
}
12+
public Pet(String name,int age,int love){
13+
super();
14+
this.name=name;
15+
this.age=age;
16+
this.love=love;
17+
}
18+
public final void print(){
19+
System.out.println("name "+name+"age"+age+"love"+love);
20+
}
21+
public void eat(){
22+
System.out.println("pet eat");
23+
}
524
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.xinan.testoop;
2+
3+
public class Test {
4+
5+
}

0 commit comments

Comments
 (0)