简单的宠物管理系统

 时间:2020-08-08  贡献者:liangxingzhijia.com

导读:宠物医院信息管理系统免费计算机毕业设计论文, 面对对象编程小实例:抽象和封装,继承,多态,接口的使用共有十一个类,其中3个测试类,4个宠物类,1个主人类,1个商店类,1个宠物父类。希望可以 帮助到大家 将他们全部进

宠物医院信息管理系统免费计算机毕业设计论文
宠物医院信息管理系统免费计算机毕业设计论文

 面对对象编程小实例:抽象和封装,继承,多态,接口的使用共有十一个类,其中3个测试类,4个宠物类,1个主人类,1个商店类,1个宠物父类。

希望可以 帮助到大家 将他们全部进行分开运行就可以看到如下运行结果: 1. TestStore运行结果: 请输入宠物名:(1.dog 2.cat 3.pig 2 喵喵喵 2. TestPet运行结果: 欢迎来到宠物店! 请输入要领养宠物的名字:哆哆 请选择要领养的宠物类型:(1.狗狗 请选择狗狗的品种:(1.拉布拉多犬 宠物自白: 我的名字叫哆哆健康值为 :100和主人的亲密度为:80,我是一只聪明的拉布拉多犬。

3. TestMaster运行结果: 给小猫喂食!健康值+4 现在的健康值为:93 狗狗吃狗粮!健康值+3 现在的健康值为:81 企鹅吃鱼!健康值+5 现在的健康值为:61 小猪吃猪食!健康值+4 现在的健康值为:93 陪主人玩飞盘~~ 健康值:78 陪主人游泳~~ 健康值:56 陪主人睡觉~~ 健康值:89 陪主人睡觉~~ 健康值:89 一、 定义动物接口Animal package Pets; /** * * @author Administrator * @Author:Irving * @Title:Animal * @Commpany: Kily * @Description: 定义动物接口Animal * @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 下午1:29:56 */ interface Animal { //定义方法shout()表示动物叫声 void shout(); } 亲密度:3 亲密度:15 亲密度:84 亲密度:84 2.企鹅 3.猪)1 2.雪纳瑞)1 4.penguin)

二、 商店测试类 package Pets; importjava.util.Scanner; publicclassTestStore { /** * @paramargs * @Title: main * @Description: 商店类测试 * @return :void * @author: 周承亮 */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub Scanner sc=newScanner(System.in); System.out.println("请输入宠物名:(1.dog 2.cat 3.pig intset=sc.nextInt(); switch (set) { case 1: Store.get("dog").shout(); break; case 2: Store.get("cat").shout(); break; case 3: Store.get("pig").shout(); break; case 4: Store.get("penguin").shout(); break; default: System.out.println("请输入相应的数字,系统退出,请重新启动"); break; } } } 三、 宠物系统测试 package Pets; importjava.util.Scanner; /** 4.penguin)");

* * @author Administrator * @Author:周承亮 * @Title:TestPet * @Commpany: Kily * @Description:宠物系统测试 * @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 下午1:30:56 */ publicclassTestPet { publicstaticvoid main(String[] args) { // TODO Auto-generated method stub Scanner input=newScanner(System.in); System.out.println("欢迎来到宠物店!"); System.out.print("请输入要领养宠物的名字:"); String name=input.next(); System.out.print("请选择要领养的宠物类型:(1.狗狗 intnum=input.nextInt(); switch(num){ case 1: String sort=""; System.out.print("请选择狗狗的品种:(1.拉布拉多犬 intDogNum=input.nextInt(); if(DogNum==1){ sort=Dog.sort1; } else{ sort=Dog.sort2; } Dog dog=newDog(name,sort); dog.setHealth(100); dog.setLove(80); dog.print(); break; case 2: String sex=""; System.out.print("请选择企鹅的性别:(1.Q仔 intPonguinNum=input.nextInt(); if(PonguinNum==1){ sex=Penguin.SEX_MALE; } 2.Q妹)"); 2.雪纳瑞)"); 2.企鹅 3.猪)");

else{ sex=Penguin.SEX_FEMALE; } Penguin QE=newPenguin(name,sex); QE.setHealth(120); QE.setLove(80); QE.print(); break; case 3: String color=""; System.out.print("请选择小猪的毛发颜色:(1.白 intPigNum=input.nextInt(); if(PigNum==1){ color=Pig.white; } else{ color=Pig.black; } Pig pig=newPig(name,color); pig.setHealth(85); pig.setLove(90); pig.print(); break; case 4: String catcolor=""; System.out.print("请选择小猪的毛发颜色:(1.白 intcatNum=input.nextInt(); if(catNum==1){ catcolor=Cat.white; } else{ catcolor=Cat.black; } Cat cat=new Cat(name,catcolor); cat.setHealth(85); cat.setLove(90); cat.print(); break; default: System.out.println("请输入相应的数字,系统退出,请重新启动"); break; } } 2.黑)"); 2.黑)");

} 四、 主人类测试 package Pets; publicclassTestMaster { /** * @Title: main * @Description: 主人类测试 * @return :void * @author: 周承亮 */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub Master master=new Master("玩物",100); Dog dog=new Dog("俄文",""); dog.setHealth(78); Penguin ponguin=new Penguin("菜鸟",Penguin.SEX_MALE); ponguin.setHealth(56); Pig pig=new Pig("毛毛",""); pig.setHealth(89); Cat cat=new Cat("毛毛",""); cat.setHealth(89); master.feed(cat); master.feed(dog); master.feed(ponguin); master.feed(pig); master.play(dog); master.play(ponguin); master.play(pig); master.play(cat); } } 五、 商店类 package Pets; /** * * @author Administrator * @Author:周承亮 * @Title:Store * @Commpany: Kily * @Description: 商店类

* @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 上午10:36:11 */ publicclassStore { publicstatic Animal get(String choice){ if (choice.equalsIgnoreCase("dog")) { returnnew Dog(); } elseif (choice.equalsIgnoreCase("cat")) { returnnew Cat(); } elseif(choice.equalsIgnoreCase("pig")){ returnnew Pig();//返回一个Pig对象 }elseif(choice.equalsIgnoreCase("penguin")){ returnnew Penguin(); } returnnull; } } 六、 宠物猪类 package Pets; /** * * @author Administrator * @Author:周承亮 * @Title:Dog * @Commpany: Kily * @Description: 猪类 * @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 下午1:29:01 */ publicclassPigextends Pet implements Animal{ static String white="白色"; static String black="黑色"; private String color; public String getColor() { returncolor; } publicvoidsetColor(String color) { this.color = color; }

public Pig() { super(); this.color="黑色"; } public Pig(String name,Stringcolor){ super.setName(name); super.setHealth(100); super.setLove(122); this.color=color; } //打印的方法 @Override publicvoid print(){ System.out.println("宠物自白:"); System.out.println("我的名字叫"+this.getName()+",健康值是 "+this.getHealth()+",和主人的亲密度是"+this.getLove()+",我的毛发颜色是 "+color); } publicvoid eat(){ if (super.getHealth()<=96) { super.setHealth(super.getHealth()+4); System.out.println("小猪吃猪食!健康值+4 现在的健康值为: "+super.getHealth()); } else { System.out.println("小猪已经是饱的啦!现在的健康值为: "+super.getHealth()); } } publicvoid Sleep(){ if (super.getHealth()>0) { super.setHealth(super.getHealth()-4); super.setLove(super.getLove()+4); System.out.println("陪主人睡觉~~ 健康值:"+super.getHealth()+" 亲密度:"+super.getLove()); } else { System.out.println("体力透支!!"); } } @Override

publicvoid shout() { // TODO Auto-generated method stub System.out.println("主人"); } } 七、 宠物父类 package Pets; /** * * @author Administrator * @Author:周承亮 * @Title:Pet * @Commpany: kily * @Description: 宠物父类 * @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 上午10:06:45 */ publicabstractclassPet {//抽象父类 private String name;//昵称 privateinthealth;//健康值 privateintlove;//亲密度 public String getName() { returnname; } publicvoidsetName(String name) { if(name==""){ this.name="无名氏"; } else{ this.name = name; } } publicintgetHealth() { returnhealth; } publicvoidsetHealth(inthealth) { if(health<1||health>100){ this.health=100; } else{ this.health = health; }

} publicintgetLove() { returnlove; } publicvoidsetLove(intlove) { if(love<1||love>100){ this.love=80; } else{ this.love = love; } } /** * * @Title: eat * @Description: 给抽象类Pet增加抽象方法eat() * @return :void * @author: 周承亮 */ publicabstractvoid eat(); publicabstractvoid print(); public Pet(){ } public Pet(String name) { this.name=name; } } 八、 宠物企鹅类 package Pets; /** * * @author Administrator * @Author:周承亮 * @Title:Dog * @Commpany: Kily * @Description: 企鹅类 * @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 下午1:29:01

*/ publicclassPenguinextends Pet implements Animal { static String SEX_MALE="Q仔"; static String SEX_FEMALE="Q妹"; private String sex;//性别 //无参的构造方法 public Penguin(){ super(); sex=SEX_MALE; } //带参的构造方法 public Penguin(String name,Stringsex){ super.setName(name); super.setHealth(22); super.setLove(10); this.sex=sex; } public String getSex(){ returnsex; } publicvoidsetSex(String sex){ this.sex=sex; } //打印的方法 publicvoid print(){ System.out.println("宠物自白:"); System.out.println("我的名字叫"+this.getName()+",健康值是 "+this.getHealth()+",和主人的亲密度是"+this.getLove()+",我是"+sex); } publicvoid eat(){ // TODO Auto-generated method stub if (super.getHealth()<=95) { super.setHealth(super.getHealth()+5); System.out.println("企鹅吃鱼!健康值+5 现在的健康值为: "+super.getHealth()); } else { System.out.println("企鹅已经是饱的啦!现在的健康值为: "+super.getHealth()); } }

publicvoid Swimming(){ if (super.getHealth()>0) { super.setHealth(super.getHealth()-5); super.setLove(super.getLove()+5); System.out.println("陪主人游泳~~ 健康值:"+super.getHealth()+" 密度:"+super.getLove()); } else { System.out.println("体力透支!!"); } } @Override publicvoid shout() { // TODO Auto-generated method stub System.out.println("qqq"); } } 九、 主人类 package Pets; /** * * @author Administrator * @Author:周承亮 * @Title:Master * @Commpany: Kily * @Description: 主人类Master * @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 下午1:35:07 */ publicclassMaster { private String name;//名字 privatedoublemoney;//元宝数 public Master(){ } public Master(String name,doublemoney){ this.name=name; this.money=money; } public String getName() { returnname; } 亲

publicvoidsetName(String name) { this.name = name; } publicdoublegetMoney() { returnmoney; } publicvoidsetMoney(doublemoney) { this.money = money; } publicvoid feed(Pet pet){ pet.eat(); } publicvoid play(Pet pet){ if(petinstanceof Dog){ ((Dog)pet).CatchFlyDish(); } elseif (petinstanceof Penguin) { ((Penguin)pet).Swimming(); }elseif (petinstanceof Cat) { ((Cat)pet).Sleep(); }else { ((Pig)pet).Sleep(); } } } 十、 宠物狗类 package Pets; /** * * @author Administrator * @Author:周承亮 * @Title:Dog * @Commpany: Kily * @Description: Dog类 * @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 下午1:29:01 */ publicclassDogextends Pet implements Animal{ static String sort1="聪明的拉布拉多犬"; static String sort2="雪纳瑞";

private String strain;//品种public String getStrain(){ returnstrain; } publicvoidsetStrain(String name,Stringkind){ super.setName(name); super.setHealth(22); super.setLove(10); this.strain=kind; } //无参的构造方法 public Dog(){ } //带参的构造方法 public Dog(String name,Stringstrain){ this.setName(name); this.strain=strain; } //打印的方法 @Override publicvoid print(){ System.out.println("宠物自白:\n我的名字叫"+this.getName()+"健康值 为 :"+this.getHealth()+"和主人的亲密度为:"+this.getLove()+", 我是一只"+strain+"。

"); } publicvoid eat() { // TODO Auto-generated method stub if (super.getHealth()<=97) { super.setHealth(super.getHealth()+3); System.out.println("狗狗吃狗粮!健康值+3 现在的健康值为: "+super.getHealth()); } else { System.out.println("狗狗已经是饱的啦!现在的健康值为: "+super.getHealth()); } } publicvoidCatchFlyDish(){

if (super.getHealth()>0) { super.setHealth(super.getHealth()-3); super.setLove(super.getLove()+3); System.out.println("陪主人玩飞盘~~ 健康值: "+super.getHealth()+" } else { System.out.println("体力透支!!"); } } @Override publicvoid shout() { // TODO Auto-generated method stub System.out.println("旺旺汪"); } } 十一、 /** * * @author Administrator * @Author:周承亮 * @Title:Dog * @Commpany: Kily * @Description: 猫类Cat,实现动物接口Animal * @Package: cn.tuling.Iving.Pets * @Create: Date 2016-1-8 * @Time: 下午1:29:01 */ publicclassCatextends Pet implements Animal{ @Override publicvoid shout() { // TODO Auto-generated method stub System.out.println("喵喵喵"); } static String white="白色"; static String black="黑色"; private String color; public String getColor() { returncolor; 宠物猫类 package Pets; 亲密度:"+super.getLove());

} publicvoidsetColor(String color) { this.color = color; } //打印的方法 @Override publicvoid print() { // TODO Auto-generated method stub System.out.println("宠物自白:"); System.out.println("我的名字是:"+getName()+"\t"+"我的健康值 是:"+getHealth()+"\t"+"我和主人的亲密度为:"+getLove()); System.out.println(",我的毛发颜色是"+color); } public Cat() { super(); this.color="黑色"; } public Cat(String name,Stringcolor){ super.setName(name); super.setHealth(100); super.setLove(122); this.color=color; } publicvoid eat(){ if (super.getHealth()<=96) { super.setHealth(super.getHealth()+4); System.out.println("给小猫喂食!健康值+4 现在的健康值为: "+super.getHealth()); } else { System.out.println("小猫已经是饱的啦!现在的健康值为: "+super.getHealth()); } } publicvoid Sleep(){ if (super.getHealth()>0) { super.setHealth(super.getHealth()-4); super.setLove(super.getLove()+4); System.out.println("陪主人睡觉~~ 健康值:"+super.getHealth()+" 密度:"+super.getLove()); } else { 亲