Java实现宠物商店管理
本文实例为大家分享了Java实现宠物商店管理的具体代码,供大家参考,具体内容如下
第一种实现方式:抽象类和对象数组
publicabstractclassAbstractPet//定义宠物模板 { privateStringname;//名称 privateStringcolor;//颜色 privateintage;//年龄 publicAbstractPet(){} publicAbstractPet(Stringname,Stringcolor,intage){ this.setName(name); this.setColor(color); this.setAge(age); } publicStringgetName(){ returnthis.name; } publicStringgetColor(){ returnthis.color; } publicintgetAge(){ returnthis.age; } publicvoidsetName(Stringname){ this.name=name; } publicvoidsetColor(Stringcolor){ this.color=color; } publicvoidsetAge(intage){ if(age>0) { this.age=age; }else{ this.age=1; } } //定义抽象方法 publicabstractvoidprintInfo();//自我介绍 }
publicclassDogextendsAbstractPet { publicDog(Stringname,Stringcolor,intage){ super(name,color,age); } //实现抽象方法 publicvoidprintInfo(){//自我介绍 System.out.println("狗:"+super.getName()+",年龄"+super.getAge()+"岁,颜色:"+super.getColor()); } }
publicclassCatextendsAbstractPet { publicCat(Stringname,Stringcolor,intage){ super(name,color,age); } //实现抽象方法 publicvoidprintInfo(){//自我介绍 System.out.println("狗:"+super.getName()+",年龄"+super.getAge()+"岁,颜色:"+super.getColor()); } }
publicclassPetShop { privateAbstractPet[]pets; privateintfoot;//定义下标 publicPetShop(intlen){//宠物数量由用户确定 if(len>0) { this.pets=newAbstractPet[len]; }else{ this.pets=newAbstractPet[1]; } } //添加宠物的方法 publicbooleanadd(AbstractPetpet){ if(this.foot publicclasstestPetShop { publicstaticvoidmain(String[]args){ PetShopp=newPetShop(5); p.add(newDog("狗1","黑色的",3)); p.add(newDog("狗2","红色的",2)); p.add(newCat("猫1","褐色的",3)); p.add(newCat("猫2","黄色的",3)); p.add(newCat("猫3","黑色的",5)); p.add(newDog("狗3","棕色的",4)); print(p.search("黑")); } publicstaticvoidprint(AbstractPetpets[]){ for(inti=0;i第二种实现方式:接口和对象数组
interfaceIPet { StringgetName();//取得宠物姓名 StringgetColor();//取得宠物颜色 intgetAge();//取得宠物年龄 voidshow();//显示宠物信息 }publicclassDogimplementsIPet { privateStringname; privateStringcolor; privateintage; publicDog(Stringname,Stringcolor,intage){ this.setName(name); this.setColor(color); this.setAge(age); } publicStringgetName(){ returnthis.name; } publicStringgetColor(){ returnthis.color; } publicintgetAge(){ returnthis.age; } publicvoidsetName(Stringname){ this.name=name; } publicvoidsetColor(Stringcolor){ this.color=color; } publicvoidsetAge(intage){ if(age<0||age>50) { this.age=1;//默认值 }else{ this.age=age; } } publicvoidshow(){ System.out.println(this.toString()); } publicStringtoString(){ return"狗:"+this.getName()+""+this.getColor()+""+this.getAge(); } }publicclassCatimplementsIPet { privateStringname; privateStringcolor; privateintage; publicCat(Stringname,Stringcolor,intage){ this.setName(name); this.setColor(color); this.setAge(age); } publicStringgetName(){ returnthis.name; } publicStringgetColor(){ returnthis.color; } publicintgetAge(){ returnthis.age; } publicvoidsetName(Stringname){ this.name=name; } publicvoidsetColor(Stringcolor){ this.color=color; } publicvoidsetAge(intage){ if(age<0||age>50) { this.age=1;//默认值 }else{ this.age=age; } } publicvoidshow(){ System.out.println(this.toString()); } publicStringtoString(){ return"猫:"+this.getName()+""+this.getColor()+""+this.getAge(); } }publicclassPetShop { privateIPet[]pets; privateintfoot; publicPetShop(intlen){//宠物店的宠物数量由用户决定 if(len>0) { pets=newIPet[len]; }else{ pets=newIPet[1];//默认最小数量为1 } } publicbooleanadd(IPetpet){ if(this.foot publicclassTestPetShop { publicstaticvoidmain(String[]args){ //创建一个宠物商店 PetShopps=newPetShop(7);//假设可以放置5只宠物 ps.add(newDog("旺旺","黑色的",4)); ps.add(newDog("旺财","白色的",6)); ps.add(newDog("小黑","黄色的",3)); ps.add(newCat("波波","褐色的",7)); ps.add(newCat("咪咪","黑色的",8)); ps.add(newCat("小云","灰色的",2)); ps.add(newDog("仔仔","黄色的",5)); print(ps.search("色")); } publicstaticvoidprint(IPet[]pet){ for(inti=0;i以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。