zoukankan      html  css  js  c++  java
  • 宠物商店—包的运用(非正确答案)

    将第六章的宠物商店代码中各个不同功能的类放在不同的包中定义,一定要严格遵循命名规范的要求。

    package PetShop.Pet.Cat;
    import PetShop.Pet.*;
    public class Cat extends Pet{
    	private String name;
    	private String color;
    	private 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;
    	}
    }
    

      

    package PetShop.Pet.Dog;
    import PetShop.Pet.*;
    public class Dog extends 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 this.name;
    	}
    	public String getColor(){
    		return this.color;
    	}
    	public int getAge(){
    		return this.age;
    	}
    }
    

      

    package PetShop.Pet;
    public abstract class Pet{
    	public abstract String getName();
    	public abstract String getColor();
    	public abstract int getAge();
    }
    

      

    package PetShop;
    import PetShop.Pet.*;
    public class PetShop{
    	private Pet[] pets;
    	private int foot;
    	public PetShop(int len){
    		if(len>0){
    			this.pets=new Pet[len];
    		}else{
    			this.pets=new Pet[1];
    		}
    	}
    	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=0;
    		for(int i=0;i<this.pets.length;i++){
    			if(this.pets[i]!=null){
    				if(this.pets[i].getName().indexOf(keyWord)!=-1||this.pets[i].getColor().indexOf(keyWord)!=-1){
    					count++;
    				}
    			}
    		}
    		p=new Pet[count];
    		int f=0;
    		for(int i=0;i<this.pets.length;i++){
    			if(this.pets[i]!=null){
    				if(this.pets[i].getName().indexOf(keyWord)!=-1||this.pets[i].getColor().indexOf(keyWord)!=-1){
    					p[f]=this.pets[i];
    					f++;
    				}
    			}
    		}
    		return p;
    	}
    }
    

      

    package PetShop;
    import PetShop.*;
    import PetShop.Pet.*;
    import PetShop.Pet.Dog.*;
    import PetShop.Pet.Cat.*;
    public class PackPetShop{
    	public static void main(String args[]){
    		PetShop ps = new PetShop(5) ;	// 五个宠物
    		ps.add(new Cat("白猫","白色的",2)) ;	// 增加宠物,成功
    		ps.add(new Cat("黑猫","黑色的",3)) ;	// 增加宠物,成功
    		ps.add(new Cat("花猫","花色的",3)) ;	// 增加宠物,成功
    		ps.add(new Dog("金毛","金色的",2)) ;	// 增加宠物,成功
    		ps.add(new Dog("黄狗","黑色的",2)) ;	// 增加宠物,失败
    		print(ps.search("黑")) ;
    	}
    	public static void print(Pet p[]){
    		for(int i=0;i<p.length;i++){
    			if(p[i]!=null){
    				System.out.println(p[i].getName() + "," + p[i].getColor()
    					+"," + p[i].getAge()) ;
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    Golang string slice
    Golang 切片
    Golang 数组
    Golang随机数
    如何才能轻松地分析日志?
    Linux 环境下 gzip 的加解密命令
    谁掳走了 nginx.pid 文件?
    这个 'ip' 竟然把我搞蒙圈了……
    Mysql 连接路径 url 参数解析
    C# 接口生成工具Swagger用法
  • 原文地址:https://www.cnblogs.com/hxtblogs/p/7649587.html
Copyright © 2011-2022 走看看