1-21作业2
1
1 package kslx.zuoye.zuoye02; 2 3 public class Test { 4 public static void main(String[] args){ 5 6 Pet dog=new Dog(); 7 if(dog instanceof Dog) { 8 ((Dog) dog).diao(); 9 }else if(dog instanceof Pen){ 10 ((Pen) dog).youyong(); 11 } 12 13 14 15 } 16 }
2
1 package kslx.zuoye.zuoye02; 2 3 public abstract class Pet { 4 private String name; 5 6 public int getHealth() { 7 return health; 8 } 9 10 public void setHealth(int health) { 11 this.health = health; 12 } 13 14 private int health; 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public void wan(){} 24 }
3
1 package kslx.zuoye.zuoye02; 2 3 public class Pen extends Pet { 4 5 public void youyong() { 6 System.out.println("南极游泳"); 7 8 } 9 }
4
1 package kslx.zuoye.zuoye02; 2 3 public class Dog extends Pet { 4 5 public void diao() { 6 System.out.println("叼飞碟..."); 7 } 8 }
5