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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
};
class Dog implements Pet{
private String name;
private String color;
private int age;
public Dog(String name,String color,int age){
this.name = name;
this.color = color;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
};
class PetShop{
private Pet[] pets;
private int foot;
public PetShop(int len){
if(len>){
this.pets = new Pet[len];
}else{
this.pets = new Pet[];
}
}
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 keyWord){
Pet p[] = null;
int count=;
for(int i=;i<this.pets.length;i++){
if(this.pets[i]!=null){
if(this.pets[i].getName().indexOf(keyWord)!=-||this.pets[i].getColor().indexOf(keyWord)!=-){
count++;
}
}
}
p = new Pet[count];
int f=;
for(int i=;i<this.pets.length;i++){
if(this.pets[i]!=null){
if(this.pets[i].getName().indexOf(keyWord)!=-||this.pets[i].getColor().indexOf(keyWord)!=-){
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 ps = new PetShop();
ps.add(new Cat("白猫","白色的",));
ps.add(new Cat("黑猫","黑色的",));
ps.add(new Cat("花猫","花色的",));
ps.add(new Dog("拉布拉多","黄色的",));
ps.add(new Dog("金毛","金色的",));
ps.add(new Dog("黄狗","黑色的",));
print(ps.search("黑"));
}
public static void print(Pet p[]){
for(int i=;i<p.length;i++){
if(p[i]!=null){
System.out.println(p[i].getName()+","+p[i].getColor()+","+p[i].getAge());
}
}
}
}

7forJava的更多相关文章

随机推荐

  1. leetcode算法之 Single Number

    题目描述: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 要求:  时间复杂度O(n),空间复杂都O(1) 示例: 输入: [2,2,1] 输 ...

  2. C++中vector,set,map自定义排序

    一.vector排序 vector支持cmp,就类似数组,可以直接sort. #include <iostream> #include <algorithm> #include ...

  3. Spring+SpringMVC+Mybatis(二)

    上一次讲的是利用mybatis提供的sqlSessionTemplate作为DAO进行数据库的操作,其实我们可以把它封装到我们自己的DAO里面,这样就是所谓的自己写DAO,这次我们写一下通过mybat ...

  4. SpringBoot向outlook发送邮件

    首先要登陆outlook邮箱,点击设置滑到最下面选择完整设置 进入后选择邮件->同步电子邮件 打开pop如上设置 下面是我的application.propertis设置 请填上自己的邮箱名与密 ...

  5. 【BGP的基本配置】

    BGP的基本配置 一:根据项目需求搭建好拓扑图如下 二:配置 1:首先进行理论分析:RT1和RT2,3分别属于不同的AS;在RT1和RT2之间建立EBGP关系,在确保RT3可以学到RT1的8.1.1. ...

  6. 如何本地搭建centos7虚拟主机?

    1 前期准备: 下载虚拟机和CentOS安装源 VMware-workstation-full-10.0.3-1895310 CentOS-7.0-1406-x86_64-DVD.iso   以下是过 ...

  7. Awakening Your Senses【唤醒你的感觉官能】

    Awakening Your Senses Give youself a test. Which way is the wind blowing? How many kinds of wildflow ...

  8. AIM Tech Round 5C. Rectangles 思维

    C. Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. go学习笔记-Data类型(Arrays, Slices and Maps)

    Data类型(Arrays, Slices and Maps) array array就是数组,定义方式如下: var arr [n]type 在[n]type中,n表示数组的长度,type表示存储元 ...

  10. office 总结

    wps word中双击格式刷即可开启永久格式刷