7forJava
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的更多相关文章
随机推荐
- Percona-Tookit工具包之pt-table-sync
Preface We've used pt-table-checksum to checksum the different table data bwtween replicatio ...
- js判断两个日期是否在几个月之内
//比较两个时间 time1,time2均为日期类型 //判断两个时间段是否相差 m 个月 function completeDate(time1 , time2 , m) { var diffyea ...
- 【ospf-路由过滤】
- Win7下如何安装python pygame的whl包
看了小甲鱼的python教程,对那个python版本的打飞机游戏很感兴趣,尝试运行,居然报错了,提示缺少pygame包: 仔细一看需要安装一个pygame的包,默认安装好python是不包括这个包的, ...
- 洛谷 P3952
题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写程序 ...
- FireDAC内存表
procedure TForm1.FormCreate(Sender: TObject); Var i:integer; begin // i:=; self.FDMemTable1.FieldDef ...
- Java+Selenium3自动化测试框架设计系列--href="javascript:void(0)"如何获得元素定位
经过前面两篇文章的铺 垫,我们这篇介绍,webdriver如何处理,一个浏览器上多个窗口之间切换的问题.我们先脑补这样一个测试场景,你在页面A点击一个连接,会在新的 tab窗口打开页面B,这个时候,你 ...
- PAT-B1032
1032 挖掘机技术哪家强(20) 输入格式: 输入在第1行给出不超过10^5^的正整数N,即参赛人数.随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号).及其比赛 ...
- IO复用——poll系统调用
1.poll函数 #include<poll.h> int poll(struct pollfd* fds, nfds_t ndfs, int timeout) poll函数在一定的时间内 ...
- 002---tcp/ip五层详解
tcp/ip 五层模型讲解 越靠底层就越接近硬件,越靠上层越接近用户.先从底层看起,理解整个互联网通信的原理. 物理层(传输电信号) 孤立的计算机想要一起玩.就必须用硬件在计算机之间完成组网.以硬件做 ...