1-21作业1
1
1 package kslx.zuoye; 2 3 import java.util.Scanner; 4 5 public class Test { 6 public static void main(String[] args){ 7 Scanner sc=new Scanner(System.in); 8 Zhuren zr=new Zhuren(); 9 Pet dog=new Dog(); 10 dog.setName("小狗"); 11 dog.setHealth(60); 12 System.out.println("当前健康值为:"+dog.getHealth()); 13 zr.chifan(dog); 14 System.out.println("当前健康值为:"+dog.getHealth()); 15 System.out.println("--------------"); 16 Pet pen=new Pen(); 17 pen.setName("Q仔"); 18 pen.setHealth(55); 19 System.out.println("当前健康值为:"+pen.getHealth()); 20 zr.chifan(pen); 21 System.out.println("当前健康值为:"+pen.getHealth()); 22 23 } 24 }
2
1 package kslx.zuoye; 2 3 public class Zhuren { 4 public void chifan(Pet pet){ 5 if(pet.getHealth()<100){ 6 pet.weishi(); 7 }else{ 8 System.out.println("健康已满,无需喂食"); 9 } 10 } 11 }
3
1 package kslx.zuoye; 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 abstract void weishi(); 24 }
4
1 package kslx.zuoye; 2 3 public class Pen extends Pet { 4 @Override 5 public void weishi() { 6 System.out.println("吃条小鱼"); 7 setHealth(getHealth()+5); 8 } 9 }
5
1 package kslx.zuoye; 2 3 public class Dog extends Pet{ 4 @Override 5 public void weishi() { 6 System.out.println("吃根骨头..."); 7 setHealth(getHealth()+3); 8 } 9 }
6