标签:注意 ryu name 运行 void close line 交流 其他
先定一个宠物的抽象类,把所有共有的属性方法放到次类中,用于子类去继承实现。
package backing2; abstract public class pet { private String name; private String sex; private int age; private int helthy; private int happy; private int hungry; private boolean alive; public abstract void play(); public abstract void yiyuango(); public abstract void youeryuango(); public abstract void youlechanggo(); public abstract void eat(); public pet(String name, String sex) { super(); this.name = name; this.sex = sex; this.age=1; this.helthy=100; this.happy=80; this.hungry=70; System.out.println("宠物名字:"+this.name+"\n"+" 性别:"+this.sex+"\t"+"年龄"+this.age+"健康值"+this.helthy+"饥饿值"+this.hungry); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getHelthy() { return helthy; } public void setHelthy(int helthy) { this.helthy = helthy; } public int getHappy() { return happy; } public void setHappy(int happy) { this.happy = happy; } public int getHungry() { return hungry; } public void setHungry(int hungry) { this.hungry = hungry; } public boolean isAlive() { return alive; } public void setAlive(boolean alive) { this.alive = alive; } }
再分别定义宠物继承小类,子类实现重写父类的所有方法,注意:方法的权限修饰符不能小于父类的权限修饰符,在子类中再定义一个方法在每次使用每个方法的时候去判断一下宠物的几个属性值
package backing2; public class qie extends pet { public qie(String name, String sex) { super(name, sex); // TODO Auto-generated constructor stub } @Override public void play() { System.out.println("小企鹅蹦蹦跳跳的玩去了"); this.setHappy(getHappy()+5); check(); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } @Override public void yiyuango() { System.out.println("小企鹅蔫蔫的去医院了"); this.setHelthy(getHelthy()+20); check(); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } @Override public void youeryuango() { System.out.println("小企鹅高高兴兴去上学"); this.setHungry(getHungry()+10); this.setHappy(getHappy()+5); check(); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } @Override public void youlechanggo() { System.out.println("小企鹅欢欢乐乐的去游乐场"); this.setHappy(getHappy()+10); check(); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } @Override public void eat() { System.out.println("小企鹅吃鱼<。)#)))≦了"); this.setHungry(getHungry()-30); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } public void check(){ if(this.getHappy()<5||this.getHungry()>90||this.getHelthy()<5){ System.out.println("您的宠物该去医院了") ; } this.getAge(); } } package backing2; public class tuzi extends pet { public tuzi(String name, String sex) { super(name, sex); // TODO Auto-generated constructor stub } @Override public void play() { check(); System.out.println("小兔子蹦蹦跳跳的玩去了"); this.setHappy(getHappy()+5); check(); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } @Override public void yiyuango() { check(); System.out.println("小兔子蔫蔫的去医院了"); this.setHelthy(getHelthy()+20); check(); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } @Override public void youeryuango() { check(); System.out.println("小兔子高高兴兴去上学"); this.setHungry(getHungry()+10); this.setHappy(getHappy()+5); check(); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } @Override public void youlechanggo() { check(); System.out.println("小兔子欢欢乐乐的去游乐场"); this.setHappy(getHappy()+10); check(); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } @Override public void eat() { check(); System.out.println("小兔子吃萝卜了"); this.setHungry(getHungry()-30); System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饥饿值"+this.getHungry()); } public void check(){ if(this.getHappy()<5||this.getHungry()>90||this.getHelthy()<5){ System.out.println("您的宠物该去医院了") ; } this.getAge(); } }
最后定义一个运行类,运行类里定义一个静态方法用来输出提示信息
package backing2; import java.util.Scanner; public class testpet { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("请输入您要选择的宠物"); System.out.println("1--兔子"); System.out.println("2--企鹅"); pet p=null; String s1=s.nextLine(); if("1".equals(s1)){ System.out.print("请输入您的宠物名字"); String s2=s.nextLine(); System.out.print("请输入您的宠物性别"); String s3=s.nextLine(); p=new tuzi(s2,s3); }else if("2".equals(s1)){ System.out.print("请输入您的宠物名字"); String s2=s.nextLine(); System.out.print("请输入您的宠物性别"); String s3=s.nextLine(); p=new tuzi(s2,s3); }else{ System.out.println("其他宠物正在孵化中。。"); } boolean falg=true; while(falg){ testpet.tishi(); String s5=s.nextLine(); if("1".equals(s5)){ p.play(); }else if("2".equals(s5)){ p.yiyuango(); }else if("3".equals(s5)){ p.youlechanggo(); }else if("4".equals(s5)){ p.eat(); }else if("5".equals(s5)){ p.youeryuango(); }else if("exit".equals(s5)){ falg=false; }else{ System.out.println("请按要求来好吗"); } }s.close(); } public static void tishi(){ System.out.println("跟宠物进行友好的交流。。"); System.out.println("1--与宠物玩"); System.out.println("2--陪宠物去医院"); System.out.println("3--陪宠物去游乐场"); System.out.println("4--陪宠物去吃饭饭"); System.out.println("5--送宠物去幼儿园"); System.out.println("exit--退出"); } }
标签:注意 ryu name 运行 void close line 交流 其他
原文地址:http://www.cnblogs.com/gaofangquan/p/7219813.html