java2--接口和链表【例】

宠物商店

//接口的使用,类的封装,链表
//2012.7.25
interface Pet{		//宠物前台
	public String getName()	;
	public String getAge();
	public String getColor();
	public String getCategory();		//抽象方法--宠物类别
};
abstract class Pets implements Pet{  					//宠物模版
	private String name;	//宠物基本属性 名字
	private String age;		//宠物基本属性 年龄
	private String pcolor;		//宠物基本属性 颜色
	public Pets(String name,String age,String pcolor){
		this.name = name;
		this.age = age;
		this.pcolor = pcolor;
	}
	public String getName(){
		return this.name;	
	}
	public String getAge(){
		return this.age;	
	}
	public String getColor(){
		return this.pcolor;	
	}	
};
class Cat extends Pets{
	public Cat(String name,String age,String pcolor){
		super(name,age,pcolor);	
	}
	public String getCategory(){
		return "cat";	
	}	
};
class Dog extends Pets{
	public Dog(String name,String age,String pcolor){
		super(name,age,pcolor);	
	}
	public String getCategory(){
		return "dog";	
	}	
};
interface PetManage{		//宠物后台管理接口
	public boolean addPet(Pet pet);
	public void getList();
	public void PetSearch(String keywords);
};
class PetOperate implements PetManage{				//宠物后台管理
	private Pet[] pets;
	private int foot;
	public PetOperate(int len){
		if(len>0){
			pets = new Pet[len];	
		}else{
			pets = new Pet[1];	
		}
	}
	public boolean addPet(Pet pet){
		if(foot<pets.length){
			pets[foot] = pet;	
			foot++;
			return true;
		}
		return false;
	}
	public void getList(){
		for(int i=0;pets[i]!=null&&i<pets.length;i++){
			System.out.println("宠物名称" + pets[i].getName() +
												"\t宠物年龄" + pets[i].getAge() + 
												"\t宠物颜色" + pets[i].getColor() +
												"\t宠物类别" + pets[i].getCategory());	
		}
	}
	public void PetSearch(String keywords){
		ListPet l = new ListPet();
		for(int i=0;i<this.pets.length;i++){
			if(this.pets[i]!=null){		// 表示此位置有宠物
				if(this.pets[i].getName().indexOf(keywords)!=-1
					||this.pets[i].getColor().indexOf(keywords)!=-1){
					l.add(this.pets[i]);
				}
			}
		}
		l.printSearchPet();
	}
};
class ListPet{				//宠物链表
	class Listview{
		private Pet pet;
		private Listview next;
		public void setPet(Pet pet){
			this.pet = pet;
		}
		public void setNext(Listview next){
		  this.next = next;	
		}
		public Listview getNext(){
			return this.next;	
		}
	}
	private Listview head ;
	public boolean add(Pet pet){
		if(head==null){
			head = new Listview();
			head.setPet(pet);
			return true;
		}else{
			return ad(pet,head);
		}
	}
	public boolean ad(Pet pet,Listview last){
		if(last.next==null){
			Listview a = new Listview();
			last.next = a;
			a.setPet(pet);
			return true;
		}else{
			return ad(pet,last.next);	
		}
	}
	public void printSearchPet(){
		if(head==null){
			return;	
		}else{
			printSearch(head);	
		}
	}
	public void printSearch(Listview last){
		System.out.println("宠物名称" + last.pet.getName() +
												"\t宠物年龄" + last.pet.getAge() + 
												"\t宠物颜色" + last.pet.getColor() +
												"\t宠物类别" + last.pet.getCategory());
		if(last.next!=null){
			printSearch(last.next);	
		}	
	}	
};
public class PetShopDemo{
	public static void main(String args[]){
		PetOperate a = new PetOperate(5);
		a.addPet(new Cat("小黄","20","蓝"));
		a.addPet(new Cat("小a","20","s"));
		a.addPet(new Dog("小d","20","e"));
		a.addPet(new Dog("小w","20","w"));	
		a.getList();
		a.PetSearch("黄");
	}	
};

宠物商店

主要还是熟练使用 接口 封装 以及链表

后来学了类集之后,又进行了修改

//宠物商店进行类集的修改
//2012.7.31
import java.util.List;
import java.util.Iterator;
import java.util.HashSet;
import java.util.Set;
import java.util.AbstractCollection;
interface Pet{		//宠物前台
	public String getName()	;
	public int getAge();
	public String getColor();
	public String getCategory();		//抽象方法--宠物类别
};
abstract class Pets implements Pet,Comparable<Pets>{  					//宠物模版
	private String name;	//宠物基本属性 名字
	private int age;		//宠物基本属性 年龄
	private String pcolor;		//宠物基本属性 颜色
	public Pets(String name,int age,String pcolor){
		this.name = name;
		this.age = age;
		this.pcolor = pcolor;
	}
	public String getName(){
		return this.name;	
	}
	public int getAge(){
		return this.age;	
	}
	public String getColor(){
		return this.pcolor;	
	}
	public String toString(){
		return 	"宠物名称" + this.getName() +
				"\t宠物年龄" + this.getAge() + 
				"\t宠物颜色" + this.getColor() +
				"\t宠物类别" + this.getCategory();
	}
	public int compareTo(Pets pet){               //加入了比较函数
		if(this.getAge()>pet.getAge()){
			return 1;	
		}else{
			return 0;
		} 
	}
	public int hashCode(){                              // 哈希值函数
		return this.name.hashCode() * this.pcolor.hashCode() * this.getAge()	;
	}	
	public boolean equals(Object obj){	          //相等判断
		String p = (String)obj;
		if(this.name.equals(p)||this.pcolor.equals(p)){
			return true;	
		}else if(Integer.parseInt(p)==this.getAge()){
			return true;	
		}else{
			return false;	
		}
	}
};
class Cat extends Pets{
	public Cat(String name,int age,String pcolor){
		super(name,age,pcolor);	
	}
	public String getCategory(){
		return "cat";	
	}	
};
class Dog extends Pets{
	public Dog(String name,int age,String pcolor){
		super(name,age,pcolor);	
	}
	public String getCategory(){
		return "dog";	
	}	
};
interface PetManage{		//宠物后台管理接口
	public void addPet(Pet pet);
	public void getList();
	public void PetSearch(String keywords);
};
class PetOperate implements PetManage{				//宠物后台管理
        //这里更改为类集的实例
        Set<Pet> pets = new HashSet<Pet>();
	public void addPet(Pet pet){
		pets.add(pet);
	}
	public void getList(){
		Iterator l = pets.iterator();
		while(l.hasNext()){
			System.out.println(l.next());
		}
	}
	
	public void PetSearch(String keywords){
		Pet[] p = pets.toArray(new Pet[pets.size()]);
		ListPet l = new ListPet();
		for(int i=0;i<p.length;i++){
			if(p[i].getName().indexOf(keywords)!=-1
				||p[i].getColor().indexOf(keywords)!=-1){
				l.add(p[i]);
			}
		}
		l.printSearchPet();
	}
};
class ListPet{				//宠物链表
	class Listview{
		private Pet pet;
		private Listview next;
		public void setPet(Pet pet){
			this.pet = pet;
		}
		public void setNext(Listview next){
		  this.next = next;	
		}
		public Listview getNext(){
			return this.next;	
		}
	}
	private Listview head ;
	public boolean add(Pet pet){
		if(head==null){
			head = new Listview();
			head.setPet(pet);
			return true;
		}else{
			return ad(pet,head);
		}
	}
	public boolean ad(Pet pet,Listview last){
		if(last.next==null){
			Listview a = new Listview();
			last.next = a;
			a.setPet(pet);
			return true;
		}else{
			return ad(pet,last.next);	
		}
	}
	public void printSearchPet(){
		if(head==null){
			return;	
		}else{
			printSearch(head);	
		}
	}
	public void printSearch(Listview last){
		System.out.println("宠物名称" + last.pet.getName() +
												"\t宠物年龄" + last.pet.getAge() + 
												"\t宠物颜色" + last.pet.getColor() +
												"\t宠物类别" + last.pet.getCategory());
		if(last.next!=null){
			printSearch(last.next);	
		}	
	}	
};
public class ListPet01{
	public static void main(String args[]){
		/*PetOperate a = new PetOperate(5);
		a.addPet(new Cat("小黄","20","蓝"));
		a.addPet(new Cat("小a","20","s"));
		a.addPet(new Dog("小d","20","e"));
		a.addPet(new Dog("小w","20","w"));	
		a.getList();
		a.PetSearch("黄");*/
		PetOperate a = new PetOperate();
		a.addPet(new Cat("小黄",20,"蓝"));
		a.addPet(new Cat("小a",20,"s"));
		a.addPet(new Dog("小d",23,"e"));
		a.addPet(new Dog("小w",21,"w"));	
		a.getList();
		a.PetSearch("小黄");
		
	}	
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值