- 积分
- 15512
- 明经币
- 个
- 注册时间
- 2008-11-24
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2012-8-5 01:42:52
|
显示全部楼层
20120805 奥运会的20公里竞走搞定金牌
宠物商店案例默写- interface Pet{
- public String getName();
- public String getColor();
- public int getAge();
- }
- class Cat implements Pet{
- String name;
- String color;
- int age;
- public Cat(String name,String color,int age){
- this.name = name;
- this.color = color;
- this.age = age;
- }
- public String getName(){
- return this.name;
- }
- public String getColor(){
- return this.color;
- }
- public int getAge(){
- return this.age;
- }
- }
- class Dog implements Pet{
- String name;
- String color;
- int age;
- public Dog(String name,String color,int age){
- this.name =name;
- this.color = color;
- this.age =age;
- }
- public String getName(){
- return this.name;
- }
- public String getColor(){
- return this.color;
- }
- public int getAge(){
- return this.age;
- }
- }
- class PetShop{
- Pet[] ps= null;
- int foot = 0;
- public PetShop(int num){
- if(num < 1){
- this.ps = new Pet[1];
- }
- else
- {
- this.ps = new Pet[num];
- }
- }
- public boolean Add(Pet ps){
- if (this.ps.length > foot){
- this.ps[foot] = ps;
- foot++;
- return true;
- }else{
- return false;
- }
- }
- public Pet[] Search(String content){
- Pet[] temp = null;
- int num =0;
- for (int i = 0;i<this.ps.length;i++){
- if(this.ps[i] != null){
- if (this.ps[i].getName().indexOf(content) != -1 ||
- this.ps[i].getColor().indexOf(content) != -1)
- {
- num++;
- }
- }
- }
- temp = new Pet[num];
- num = 0;
- for (int i = 0;i<this.ps.length;i++){
- if(this.ps[i] != null){
- if (this.ps[i].getName().indexOf(content) != -1 ||
- this.ps[i].getColor().indexOf(content) != -1)
- {
- temp[num] = ps[i];
- num++;
- }
- }
- }
- return temp;
- }
- }
- public class Test{
- public static void main(String[] args){
- PetShop ps = new PetShop(5);
- ps.Add(new Cat("白猫","2",2));
- ps.Add(new Cat("黑猫","2",2));
- ps.Add(new Dog("白狗","2",2));
- ps.Add(new Cat("1","白色",2));
- ps.Add(new Cat("1","2",2));
- ps.Add(new Cat("1","2",2));
- Print(ps.Search("白"));
- }
- public static void Print(Pet ps[]){
- for (int i = 0;i<ps.length;i++){
- System.out.print(ps[i].getName() + "\n");
- }
- }
- }
复制代码 |
|