Java实现一个宠物商店管理?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

第一种实现方式:抽象类和对象数组
public abstract class AbstractPet //定义宠物模板
{
private String name; //名称
private String color; //颜色
private int age; //年龄
public AbstractPet(){}
public AbstractPet(String name, String color, int age){
this.setName(name);
this.setColor(color);
this.setAge(age);
}
public String getName(){
return this.name;
}
public String getColor(){
return this.color;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name = name;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
if (age > 0)
{
this.age = age;
}else{
this.age = 1;
}
}
//定义抽象方法
public abstract void printInfo(); //自我介绍
}