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的更多相关文章
随机推荐
- flask笔记(一)
1.第一个flask项目 # 首先你要安装flask这个模块 pip install flask # 安装好了之后,直接新建一个py文件,开始写最简单的flask项目了 from flask impo ...
- Spring+SpringMVC+Mybatis(二)
上一次讲的是利用mybatis提供的sqlSessionTemplate作为DAO进行数据库的操作,其实我们可以把它封装到我们自己的DAO里面,这样就是所谓的自己写DAO,这次我们写一下通过mybat ...
- Linux 学习第二天
一.常用命令的使用 1.echo echo https://www.cnblogs.com/rise-home/ 输出字符串 2.ps -aux(显示进程信息) ps 进程状态共有5种 A.STAT ...
- mysql创建表时反引号的作用
试用navicat工具查看现网mysql建表语句时,发现表名和字段名都是反引号引起来的 CREATE TABLE `tab_notice_title_tv` ( `i_id` ) NOT NULL A ...
- CSS3--j惊艳到你的新前端
一.css3的选择器 1. 父子选择器 直接关系 .box>.com 2. 兄弟选择器 相邻关系 .box+.com <span>hello</span> <p&g ...
- jQuery 使用问题
attr('checked', 'checked')调用多次仅第一次生效 使用attr()获取这些属性的返回值为String类型,如果被选中(或禁用)就返回checked.selected或disab ...
- 【Thrift一】Thrift安装部署
Thrift安装部署 Thrift安装部署 下载源码包 安装g++ 解压Thrift安装包 安装boost开发工具 测试(python版) 下载源码包 wget http://apache.fayea ...
- 字典树(Trie)的学习笔记
按照一本通往下学,学到吐血了... 例题1 字典树模板题吗. 先讲讲字典树: 给出代码(太简单了...)! #include<cstdio> #include<cstring> ...
- GDOI DAY1游记
今天,是本蒟蒻的第一次参加GDOI,真激动! 今天,是GDOI第一天,昨天熬夜打代码,今天早上状态十分不好,于是... 进入了考场,叫我们自由打一会代码,于是...打了一坨AC机,重要的是错了(额.. ...
- mongodb的学习之旅一
描述 作为一枚菜鸟级别的coder,刚接触nodejs没有多久.现在在学习微信公众号的开发,但是碰到了mongodb保存用户数据的时候,出现了DeprecationWarning: Mongoose: ...