【本節目标】
通過閱讀本節内容,你将通過編碼實作寵物商店的情景,進一步複習接口的相關知識,進一步掌握連結清單的結構與功能實作以及使用連結清單解決實際問題的能力。
綜合實戰:寵物商店
寵物商店:
現在假設一個寵物商店,裡面可以出售各種寵物,要求可以實作寵物的上架,下架處理,也可以根據關鍵字查詢寵物的資訊。

圖一 寵物設計實作
步驟:
1、應該定義出寵物的标準
2、定義寵物商店
3、根據寵物标準定義寵物資訊(定義寵物貓、寵物狗)
4、實作寵物商店的操作
interface ILink<E> { //設定泛型避免安全隐患
public void add(E e) ; //增加資料
public int size() ; //擷取資料的個數
public boolean isEmpty() ; //判斷是否空集合
public Object[] toArray() ; //将集合元素以數組的形式傳回
public E get(int index) ; //根據索引擷取資料
public void set(int index,E data) ; //修改索引資料
public boolean contains(E data) ; //判斷資料是否存在
public void remove(E e) ; //資料删除
public void clean() ; //清空集合
}
class LinkImpl<E> implements ILink<E> {
private class Node { //儲存節點的資料關系
private E data ; //儲存資料
private Node next ; //儲存下一個引用
public Node(E data) { //有資料的情況下才有意義
this.data = data ;
}
//第一次調用:this = LinkImpl.root ;
//第二次調用:this = LinkImpl.root.next ;
//第三次調用:this = LinkImpl.root.next.next ;
public void addNode(Node newNode){ //儲存新的Node資料
if (this.next == null) { //目前節點的下一個節點為null
this.next = newNode; //儲存目前節點
}else {
this.next.addNode(newNode);
}
}
//第一次調用:this = LinkImpl.root
//第二次調用:this = LinkImpl.root.next
/第三次調用:this = LinkImpl.root.next.next
public void toArrayNode() {
LinkImpl.this.returnData [LinkImpl.this.foot ++] = this.data ;
if (this.next != null) { //還有下一個資料
this.next.toArrayNode() ;
}
}
public E getNode(int index) {
if (LinkImpl.this.foot ++ == index) { //索引相同
return this.data ; //傳回目前資料
}else {
return this.next.getNode(index) ;
}
}
public void setNode(int index,E data) {
if (LinkImpl.this.foot ++ == index) { //索引相同
this.data = data ; //修改資料
}else {
this.next.setNode(index,data) ;
}
}
public boolean containsNode(E data) {
if (data.equals(this.data)) { //對象比較
return true ;
}else {
if (this.next == null) { //沒有後續節點
return false ; //找不到
}else {
return this.next.containsNode(data) ; //向後繼續判斷
}
}
}
public void removeNode(Node<E> previous,E date) {
if (this.date.equals(date)) {
previous.next = this.next ;
}else {
if (this.next!=null) {
this.next.removeNode(this,date) ;
}
}
}
public void removeNode (Node previous,E data) {
if (this.data.equals(data)) {
previous.next = this.next ; //空出目前節點
}else {
if (this.next != null) { //有後續節點
this.next.removeNode(this, data) ; //向後繼續删除
}
}
}
}
//------------以下為Link類中定義的成員-----------------
private Node root ; //儲存根元素
private int count ; //儲存資料的個數
private int foot ; //描述的是操作數組的腳标
private Object[] returnData ; //傳回的資料儲存
//------------以下為Link類中定義的方法-----------------
public void add(E e){
if(e == null){
return ;
}
//資料本身是不具有關聯特性的,隻有Node類有,要想關聯處理就必須将資料包裝在Node類中
Node newNode = new Node(e); //建立一個新的節點
if (this.root == null){ //現在沒有根節點
this.root = newNode; //第一個節點作為根節點
}else{ //根節點存在
this.root.addNode(newNode); //将新節點儲存在合适的位置
}
this.count++ ;
}
public int size() {
return this.count ;
}
public boolean isEmpty() {
//return this.root == null ;
return this.count == 0 ;
}
public Object[] toArray() {
if (this.isEmpty()) { //空集合
return null ; //現在沒有資料
}
this.foot = 0 ; //腳标清零
this.returnData = new Object[this.count] ; //根據已有的長度開辟數組
this.root.toArrayNode() ; //利用Node類進行遞歸資料擷取
return this.returnData ;
}
public E get(int index) {
if (index >= this.count) { //索引應該在指定的範圍之内
return null ;
} //索引資料的擷取應該由Node類完成
this.foot = 0 ; //重置索引的下标
return this.root.getNode(index) ;
}
public void set(int index,E data) {
if (index >= this.count) { //索引應該在指定的範圍之内
return ; //方法結束
} //索引資料的擷取應該由Node類完成
this.foot = 0 ; //重置索引的下标
this.root.setNode(index,data) ; //修改資料
}
public boolean contains(E data) {
if (data == null) {
return false ; //沒有資料
}
return this.root.containsNode(data) ; //交給Node類判斷
}
public void remove(E data) {
if (this.contains(data)) { //判斷資料是否存在
if (this.root.data.equals(data)) { //根節點為要删除節點
this.root = this.root.next ; //根的下一個節點
}else { //交由Node類進行删除
this.root.next.removeNode(this.root , data) ;
}
this.count -- ;
}
}
public void clean() {
this.root = null ; //後續的所有節點都沒了
this.count = 0 ; //個數清零
}
}
interface Pet{ //定義寵物标準
public String getName(); //獲得名字
public String getColor(); //獲得顔色
}
class Petshop{ //寵物商店
private ILink<Pet> allPets = new LinkImpl<Pet>(); //儲存多個寵物
public void add(Pet pet) { //追加寵物,商品上架
this.allPets.add(pet); //集合中儲存對象
}
public void delete(Pet pet) {
this.allPets.remove(pet);
}
public ILink<Pet> search(String keyword){
ILink<Pet> searchresult = new LinkImpl<Pet>(); //儲存查詢結果
Object result[] = this.allPets.toArray(); //擷取全部資料
if(result !=null ) {
for(Object obj:result) {
Pet pet = (Pet)obj;
if(pet.getName().contains(keyword)||
pet.getColor().contains(keyword)) {
searchresult.add(pet); //儲存查詢結果
}
}
}
return searchresult;
}
}
class Cat implements Pet{ //實作寵物标準
private String name;
private String color;
public Cat(String name,String color) {
this.name = name;
this.color = color;
}
public String getName() {
return this.name;
}
public String getColor() {
return this.color;
}
public boolean equals(Object obj) {
if(obj == null) {
return false ;
}
if (!(obj instanceof Cat)) {
return false ;
}
if (this == obj) {
return true ;
}
Cat cat = (Cat) obj ;
return this.name.equals(cat.name) && this.color.equals(cat.color) ;
}
public String toString() {
return "【寵物貓】名字:"+this.name+"、顔色:"+this.color;
}
}
class Dog implements Pet{
private String name;
private String color;
public Dog(String name,String color) {
this.name = name;
this.color = color;
}
public String getName() {
return this.name;
}
public String getColor() {
return this.color;
}
public boolean equals(Object obj) {
if(obj == null) {
return false ;
}
if (!(obj instanceof Dog)) {
return false ;
}
if (this == obj) {
return true ;
}
Dog dog = (Dog) obj ;
return this.name.equals(dog.name) && this.color.equals(dog.color) ;
}
public String toString() {
return "【寵物狗】名字:"+this.name+"、顔色:"+this.color;
}
}
public class ListCon{
public static void main(String args[]) {
Petshop shop = new Petshop(); //開店
shop.add(new Dog("黃斑狗","綠色"));
shop.add(new Cat("小強貓","深綠色"));
shop.add(new Dog ("黃貓","深色"));
shop.add(new Dog ("黃狗","黃色"));
shop.add(new Dog ("斑點狗","灰色"));
Object result[] = shop.search("黃").toArray();
for(Object obj : result) {
System.out.println(obj);
}
}
}
圖二 執行結果圖
所有的程式開發都是以接口為标準的,這樣在進行後期程式處理的時候就可以非常靈活,隻要符合标準的對象都可以儲存。
想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。
本内容視訊來源于
阿裡雲大學 下一篇:連結清單實戰之超市購物車 | 帶你學《Java面向對象程式設計》之九十六 更多Java面向對象程式設計文章檢視此處