Java继承与多态的简单应用–宠物商店
代码演示
package chapter_six;
//定义宠物接口,所有宠物都需要实现此接口
interface Pet{
public String getName(); //获取宠物的名字
public String getColor(); //得到宠物的颜色
public int getAge(); //得到宠物的年龄
}
//定义Cat猫的类,实现Pet宠物接口
class Cat implements Pet{
private String name; //定义名字name
private String color; //定义颜色color
private int age; //定义年龄age
public Cat(String name, String color, int age){ //通过构造方法为Cat猫类传入参数
this.name = name; //传入name名称
this.color = color; //传入颜色color
this.age = age; //传入年龄age
}
//设置属性的setter方法
public void setName(String name) {
this.name = name;
}
public void setColor(String color) {
this.color = color;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String getName() {
return this.name;
} //覆写getName()方法
@Override
public String getColor() {
return this.color;
} //覆写getColor()方法
@Override
public int getAge() {
return this.age;
} //覆写getAge()方法
}
//定义实现宠物接口的Dog狗类
class Dog implements Pet{
private String name; //定义名字name
private String color; //定义颜色color
private int age; //定义年龄age
public Dog(String name, String color, int age){ //通过Dog狗类的构造方法传入参数
this.name = name; //设置名字name
this.color = color; //设置颜色color
this.age = age; //设置年龄age
}
//Dog狗类属性的setter方法
public void setName(String name) {
this.name = name;
}
public void setColor(String color) {
this.color = color;
}
public void setAge(int age) {
this.age = age;
}
//覆写获取属性的getter()方法
@Override
public String getName() {
return this.name;
}
@Override
public String getColor() {
return this.color;
}
@Override
public int getAge() {
return this.age;
}
}
//定义宠物商店类
class PetShop{
private Pet[] pets; //定义宠物接口对象数组,以便传递实例进行向上转型
private int foot; //定义数组的下标foot
public PetShop(int len){ //PetShop类的构造方法,传入对象数组的初始长度
if(len > 0){ //若参数大于0
this.pets = new Pet[len]; //实例化对象数组,长度为len
}else{
this.pets = new Pet[1]; //否则建立长度为1的对象数组
}
}
/**
* 增加宠物的方法,传入的参数为Pet接口的实例,即实现Pet接口的所有对象
* 增加成功返回true,否则返回false
* @param pet
* @return
*/
public boolean add(Pet pet){
if(this.foot < this.pets.length){ //若数组下标未超过数组长度,说明仍能增加宠物
this.pets[this.foot] = pet; //为宠物商店增加宠物,宠物为传输进来的Pet接口实例
this.foot++; //数组下标累加
return true; //添加宠物成功,返回true
}else{
return false; //宠物商店已存满宠物则返回false
}
}
/**
* 定义宠物查找方法,通过输入关键字,查找出所有符合的宠物
* 以对象数组的形式返回
* @param key_word
* @return
*/
public Pet[] search(String key_word){
Pet[] p = null; //初始化对象数组为null
int count = 0; //初始化对象数组中对象的个数为0
for(int i = 0; i < this.pets.length; i++){ //循环遍历对象数组(所有宠物)
if(this.pets[i] != null){ //若此宠物对象不为null
if(this.pets[i].getName().indexOf(key_word) != -1 //进行关键字匹配,匹配信息为名字或颜色
||this.pets[i].getColor().indexOf(key_word) != -1){
count++; //若匹配成功,则个数累加
}
}
}
p = new Pet[count]; //循环遍历结束后,以符合条件的个数实例化对象数组
int f = 0; //定义新对象数组下标为0
for (int i = 0; i < this.pets.length; i++){ //再次对对象数组(宠物商店)进行遍历
if(this.pets[i].getName().indexOf(key_word) !=-1
|| this.pets[i].getColor().indexOf(key_word) != -1){ //进行关键字匹配,匹配信息为名字或颜色
p[f] = this.pets[i]; ///匹配成功则将此宠物添加至新的对象数组中
f++; //下标累加
}
}
return p; //返回搜寻出的结果
}
}
public class Pag224 {
public static void main(String[] args) {
PetShop ps = new PetShop(5);
ps.add(new Cat("白猫","白色的",2)); //实例化多种宠物,并将它们放入宠物商店
ps.add(new Cat("黑猫","黑色的",3));
ps.add(new Cat("花猫","花色的",3));
ps.add(new Cat("拉布拉多","黄色的",3));
ps.add(new Cat("金毛","金色的",2));
ps.add(new Cat("黄狗","黑色的",2)); //由于宠物商店最多能存放5只狗,所以存放最后一只时失败
Pet[] pets = ps.search("黑"); //以"黑"关键字进行检索查找,返回查找结果
for(int i = 0; i < pets.length; i++){
if(pets[i] != null){ //输出所有查询出的结果
System.out.println(pets[i].getName() + "\n"
+ pets[i].getColor() + "\n" + pets[i].getAge());
}
}
}
}
运行结果
黑猫
黑色的
3