鍊錶的綜合案例 寵物商店

2021-10-10 08:15:38 字數 3996 閱讀 6544

綜合案例:寵物商店

現在假設有乙個寵物商店,裡面可以**各種寵物,要求可以實現寵物的上架處理、下架處理,也可以根據關鍵字查詢出寵物的資訊。

1、應該定義出寵物的標準

inte***ce pet
2、寵物商店應該以'寵物的標準為主;

class petshop 

public

void

delete(pet pet)

public ilinksearch(string keyword)

}} return searchresult;

}}

3、根據寵物的標準來定義寵物資訊

定義寵物貓:

定義寵物狗:

class cat implements pet

public string getname()

public string getcolor()

public boolean equals(object obj)

if(!(obj instanceof cat))

if (this == obj)

cat cat = (cat) obj;

return this.name.equals(cat.name) && this.color.equals(cat.color);

}public string tostring()

}class dog implements pet

public string getname()

public string getcolor()

public boolean equals(object obj)

if(!(obj instanceof cat))

if (this == obj)

dog dog = (dog) obj;

return this.name.equals(dog.name) && this.color.equals(dog.color);

}public string tostring()

}4、實現寵物商店的操作

public class petdemo 

}}

所有的程式開發都是以介面為標準進行的,這樣在進行後期程式處理的時候就可以非常的靈活,只要符合標準的物件都可以儲存。 

完整原始碼:

inte***ce ilink

class linkimplimplements ilink

//第一次呼叫:this =linkimpl.root;

//第二次呼叫:this =linkimpl.root.next;

//第三次呼叫: this = linkimpl.root.next.next;

public void addnode(node newnode) else

} //第一次呼叫:this =linkimpl.root

//第二次呼叫:this =linkimp .root.next

//第三次呼叫: this =linkimp.root.next.next

public void toarraynode ()

} public e getnode (int index)else

} public void setnode(int index,e data) else

} public boolean containsnode (e data)else else

}} public void removenode (node previous,e data)else

}} }

//-------------- 以下為link類中定義的成員 --------------

private node root;//儲存根元素

private int count;//儲存資料個數

private int foot ; //描述的是運算元組的腳標

private object returndata ; //返回的資料儲存

//-------------- 以下為link類中定義的方法 --------------

public void add(e e)

//資料本身是不具有關聯特性的,只有node類有,那麼要想實現關聯處理就必須將資料報裝在node之中

node newnode = new node (e) ; //建立乙個新的節點

if (this.root == null )else

this.count++;

} public int size()

public boolean isempty()

public object toarray()

this.foot = 0 ; //腳標清零

this.returndata = new object [this.count] ;//根據已有的長度開闢陣列

this.root. toarraynode ();//公利用node類進行遞迴資料獲取

return this.returndata ;

} public e get(int index)//索引資料的獲取應該由node類完成

this.foot =0 ; //重置索引的下標

return this.root.getnode(index);

} public void set(int index,e data) //索引資料的獲取應該由node類完成

this.foot =0 ; //重置索引的下標

this.root.setnode(index,data);//修改資料

} public boolean contains(e data)

return this.root.containsnode (data) ;//交給node天判斷

} public void remove(e data) else

this.count --;

} }public void clean()

}inte***ce pet

class petshop

public void delete(pet pet)

public ilinksearch(string keyword)

}} return searchresult; }}

class cat implements pet

public string getname()

public string getcolor()

public boolean equals(object obj)

if(!(obj instanceof cat))

if (this == obj)

cat cat = (cat) obj;

return this.name.equals(cat.name) && this.color.equals(cat.color);

} public string tostring()

}class dog implements pet

public string getname()

public string getcolor()

public boolean equals(object obj)

if(!(obj instanceof cat))

if (this == obj)

dog dog = (dog) obj;

return this.name.equals(dog.name) && this.color.equals(dog.color);

} public string tostring()

}public class petdemo

}}

Java Object綜合實戰 寵物商店

鍊錶結構 class link 1.新增節點 public void addnode node newnode else 2.資料查詢 public boolean containsnode object data else else 3.修改資料 public void setnode int i...

java 用鍊錶實現寵物商店的一些基本操作

要求實現以下需求 特別要求 node中儲存的資料data型別為object。初版 如下 class link public node object data public void removenode object keyword else now now.next return public v...

鍊錶的綜合使用

鍊錶的使用 建立 include 分配空間 define null 0 define len sizeof struct student struct student 定義結構體 int n 全域性變數,記錄結點個數 struct student creat void p2 next null re...