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("小黃");
		
	}	
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章