标签:Java String int 宠物 color 宠物商店 public name
interface Pet{ // 定义宠物接口
public String getName() ;
public String getColor() ;
public int getAge() ;
}
class Cat implements Pet{ // 猫是宠物,实现接口
private String name ; // 宠物名字
private String color ; // 宠物颜色
private int age ; // 宠物年龄
public Cat(String name,String color,int age){
this.setName(name) ;
this.setColor(color) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class Dog implements Pet{ // 狗是宠物,实现接口
private String name ; // 宠物名字
private String color ; // 宠物颜色
private int age ; // 宠物年龄
public Dog(String name,String color,int age){
this.setName(name) ;
this.setColor(color) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class PetShop{ // 宠物商店
private Pet[] pets ; // 保存一组宠物
private int foot ;
public PetShop(int len){
if(len>0){
this.pets = new Pet[len] ; // 开辟数组大小
}else{
this.pets = new Pet[1] ; // 至少开辟一个空间
}
}
public boolean add(Pet pet) throws Exception{ // 增加的是一个宠物
if(this.foot<this.pets.length){
this.pets[this.foot] = pet ; // 增加宠物
this.foot ++ ;
return true ;
}else{
return false ;
}
}
public Pet[] search(String keyWord) throws Exception{
// 应该确定有多少个宠物符合要求
Pet p[] = null ;
int count = 0 ; // 记录下会有多少个宠物符合查询结果
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){ // 表示此位置有宠物
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
count++ ; // 修改查找到的记录数
}
}
}
p = new Pet[count] ; // 开辟指定的大小空间
int f = 0 ; // 增加元素的位置标记
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){ // 表示此位置有宠物
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
p[f] = this.pets[i] ;
f++ ;
}
}
}
return p ;
}
};
public class PetShopDemo{
public static void main(String args[]){
PetShop ps = new PetShop(5) ; // 五个宠物
try{
ps.add(new Cat("白猫","白色的",2)) ; // 增加宠物,成功
ps.add(new Cat("黑猫","黑色的",3)) ; // 增加宠物,成功
ps.add(new Cat("花猫","花色的",3)) ; // 增加宠物,成功
}catch(Exception e){
e.printStackTrace();
}
// ps.add(new Dog("拉步拉多","黄色的",3)) ; // 增加宠物,成功
// ps.add(new Dog("金毛","金色的",2)) ; // 增加宠物,成功
// ps.add(new Dog("黄狗","黑色的",2)) ; // 增加宠物,失败
try{
print(ps.search("白")) ;
}catch(Exception e){
e.printStackTrace();
}
}
public static void print(Pet p[]){
for(int i=0;i<p.length;i++){
if(p[i]!=null){
System.out.println(p[i].getName() + "," + p[i].getColor()
+"," + p[i].getAge()) ;
}
}
}
};
标签:Java,String,int,宠物,color,宠物商店,public,name 来源: https://blog.csdn.net/qq_58157968/article/details/117594158
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。