interface Pet {
// 定义宠物接口
public String getName();
public int getAge();
public String getColor();
}
class Cat implements Pet {
// 定义属性
private String name;
private int age;
private String color;
// 通过构造方法为属性赋值
public Cat(String name, int age, String color) {
super();
this.setName(name);
this.setAge(age);
this.setColor(color);
}
// setter getter 方法
public void setName(String name) {
this.name = name;
}
public void setAge( int age) {
this.age = age;
}
public void setColor(String color) {
this.color = color;
}
// 实现接口中的方法
@Override
public int getAge() {
// 即获取的年龄为本类的年龄
return this.age;
}
@Override
public String getColor() {
// 获取的颜色为本类颜色
return this.color;
}
@Override
public String getName() {
// 获取的名字为本类的名字
return this.name;
}
}
class Dog implements Pet{
private String name;
private int age ;
private String color;
public Dog(String name, int age, String color) {
super();
this.setName(name);
this.setAge(age);
this.setColor(color);
}
public void setName(String name) {
this.name = name;
}
public void setAge( int age) {
this.age = age;
}
public void setColor(String color) {
this.color = color;
}
@Override
public int getAge() {
return this.age;
}
@Override
public String getColor() {
return this.color;
}
@Override
public String getName() {
return this.name ;
}
}
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) {
if ( this.foot < this.pets.length) { // 如果下标小于数组长度
this.pets[ this.foot] = pet; // 增加宠物
this.foot++; // 不断添加
return true;
} else {
return false;
}
}
public Pet[] search(String keyWords) {
// 关键字查找,确定有多少个宠物符合标准
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(keyWords) != -1
|| this.pets[i].getColor().indexOf(keyWords) != -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(keyWords) != -1
|| this.pets[i].getColor().indexOf(keyWords) != -1) {
p[f] = this.pets[i];
f++; // 修改找到的记录数
}
}
}
return p;
}
}
public class PetShopDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PetShop petShop = new PetShop(5); // 定义宠物商店有5个元素
//添加操作
petShop.add( new Cat( "蓝猫", 20, "蓝色"));
petShop.add( new Dog( "红狗", 20, "红色"));
petShop.add( new Dog( "花狗", 20, "花色"));
petShop.add( new Cat( "蓝猫", 20, "蓝色"));
petShop.add( new Dog( "红狗", 20, "红色"));
petShop.add( new Dog( "花狗", 20, "花色")); //添加失败
print(petShop.search( "红"));
}
public static void print(Pet pet[]) {
for ( int i = 0; i < pet.length; i++) {
if (pet[i] != null) {
System.out.println(pet[i].getName() + "," + pet[i].getAge()
+ "," + pet[i].getColor() + "\n");
}
}
}
}
public String getName();
public int getAge();
public String getColor();
}
class Cat implements Pet {
// 定义属性
private String name;
private int age;
private String color;
// 通过构造方法为属性赋值
public Cat(String name, int age, String color) {
super();
this.setName(name);
this.setAge(age);
this.setColor(color);
}
// setter getter 方法
public void setName(String name) {
this.name = name;
}
public void setAge( int age) {
this.age = age;
}
public void setColor(String color) {
this.color = color;
}
// 实现接口中的方法
@Override
public int getAge() {
// 即获取的年龄为本类的年龄
return this.age;
}
@Override
public String getColor() {
// 获取的颜色为本类颜色
return this.color;
}
@Override
public String getName() {
// 获取的名字为本类的名字
return this.name;
}
}
class Dog implements Pet{
private String name;
private int age ;
private String color;
public Dog(String name, int age, String color) {
super();
this.setName(name);
this.setAge(age);
this.setColor(color);
}
public void setName(String name) {
this.name = name;
}
public void setAge( int age) {
this.age = age;
}
public void setColor(String color) {
this.color = color;
}
@Override
public int getAge() {
return this.age;
}
@Override
public String getColor() {
return this.color;
}
@Override
public String getName() {
return this.name ;
}
}
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) {
if ( this.foot < this.pets.length) { // 如果下标小于数组长度
this.pets[ this.foot] = pet; // 增加宠物
this.foot++; // 不断添加
return true;
} else {
return false;
}
}
public Pet[] search(String keyWords) {
// 关键字查找,确定有多少个宠物符合标准
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(keyWords) != -1
|| this.pets[i].getColor().indexOf(keyWords) != -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(keyWords) != -1
|| this.pets[i].getColor().indexOf(keyWords) != -1) {
p[f] = this.pets[i];
f++; // 修改找到的记录数
}
}
}
return p;
}
}
public class PetShopDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PetShop petShop = new PetShop(5); // 定义宠物商店有5个元素
//添加操作
petShop.add( new Cat( "蓝猫", 20, "蓝色"));
petShop.add( new Dog( "红狗", 20, "红色"));
petShop.add( new Dog( "花狗", 20, "花色"));
petShop.add( new Cat( "蓝猫", 20, "蓝色"));
petShop.add( new Dog( "红狗", 20, "红色"));
petShop.add( new Dog( "花狗", 20, "花色")); //添加失败
print(petShop.search( "红"));
}
public static void print(Pet pet[]) {
for ( int i = 0; i < pet.length; i++) {
if (pet[i] != null) {
System.out.println(pet[i].getName() + "," + pet[i].getAge()
+ "," + pet[i].getColor() + "\n");
}
}
}
}
转载于:https://blog.51cto.com/haiyuanxi/924971