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的更多相关文章
随机推荐
- Webpack Tapable原理详解
directory - src - sim ---- 简单的模拟实现 - /.js$/ ---- 使用 代码已上传github, 地址 Detailed Webpack 就像一条生产线, 要经过一系列 ...
- 重写Alert和confirm方法去除地址显示
//重写alert方法,去掉地址显示window.alert = function(name){var iframe = document.createElement("IFRAME&quo ...
- springMVC中接收请求参数&&数据转发
### 1. 接收请求参数 #### 1.1. [不推荐] 通过HttpServletRequest获取请求参数 假设存在: <form action="handle_login.do ...
- 线程池的类型以及执行线程submit()和execute()的区别
就跟题目说的一样,本篇博客,本宝宝主要介绍两个方面的内容,其一:线程池的类型及其应用场景:其二:submit和execute的区别.那么需要再次重申的是,对于概念性的东西,我一般都是从网上挑选截取,再 ...
- 高级同步器:信号量Semaphore
引自:https://blog.csdn.net/Dason_yu/article/details/79734425 一.信号量一个计数信号量.从概念上讲,信号量维护了一个许可集.Semaphore经 ...
- 【nat---basic,napt,easy ip】
display nat :显示nat 信息 debugging nat :对nat进行调试 reset nat session:擦除nat连接配置 basic-nat:公网->私网(一对一) n ...
- JavaScript 转载
JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...
- 爬虫之爬取斗鱼官网LOL部分主播的状态
一个爬虫小程序 爬取主播的排名及观看人数 import re import requests import request class Spider(): url = 'https://www.dou ...
- gvim编码配置知识
配置 .vimrc 解决 Vim / gVim 在中文 Windows 下的字符编码问题 Vim / gVim 在中文 Windows 下的字符编码有两个问题: 默认没有编码检测功能 如果一个 ...
- 按升序打印X,Y,Z的整数值
#include <stdio.h> #define TRUE 1 #define FALSE 0 int main() { int x,y,z; printf("x: &quo ...