宠物商店 - MLDN 李兴华老师
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                宠物商店 - MLDN 李兴华老师
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                根據(jù)視頻整理
?
class Link {class Node {private Node next;// 下一個節(jié)點private Object data;// 數(shù)據(jù)public Node(Object data) {this.data = data;}// 1.添加節(jié)點public void addNode(Node newNode) {if (this.next == null) {this.next = newNode;} else {this.next.addNode(newNode);}}// 2.數(shù)據(jù)查詢public boolean containsNode(Object data) {if (data.equals(this.data)) {return true;} else {if (this.next != null) {return this.next.containsNode(data);} else {return false;}}}// 3.修改數(shù)據(jù)public void setNode(int index, Object data) {if (Link.this.foot++ == index) {this.data = data;}this.next.setNode(index, data);}// 4.獲取數(shù)據(jù)public Object getData(int index) {if (Link.this.foot++ == index) {return this.data;}return this.next.getData(index);}// 5.刪除數(shù)據(jù)public void removeNode(Node previous, Object data) {if (data.equals(this.data)) {previous.next = this.next;} else {this.next.removeNode(this, data);}}// 6.對象數(shù)組public void toArrayNode() {Link.this.retArray[Link.this.foot++] = this.data;if (this.next != null) {this.next.toArrayNode();}}}// ===================以上是內(nèi)部類=========================private Node root;// 根節(jié)點private int count;// 計數(shù)器private int foot;// 腳標(biāo)private Object[] retArray;// 對象數(shù)組// 1.添加數(shù)據(jù)public void add(Object data) {if (data == null) {return;}Node newNode = new Node(data);// 數(shù)據(jù)裝包if (this.root == null) {this.root = newNode;} else {this.root.addNode(newNode);}this.count++;}// 2.鏈表長度public int size() {return this.count;}// 3.鏈表是否為空public boolean isEmpty() {return this.count == 0;}// 4.鏈表查詢public boolean contains(Object data) {if (data == null || this.root == null) {return false;}return this.root.containsNode(data);}// 5.修改數(shù)據(jù)public void set(int index, Object data) {if (index > this.count) {return;}this.foot = 0;this.root.setNode(index, data);}// 6.獲取數(shù)據(jù)public Object get(int index) {if (index > this.count) {return null;}this.foot = 0;return this.root.getData(index);}// 7.刪除數(shù)據(jù)public void remove(Object data) {if (this.contains(data)) {if (data.equals(this.root.data)) {this.root = this.root.next;} else {this.root.next.removeNode(this.root, data);}}this.count--;}// 8.對象數(shù)組public Object[] toArray() {if (this.root == null) {return null;}this.retArray = new Object[this.count];this.root.toArrayNode();return this.retArray;}}interface Pet{//定義寵物接口public String getName();//定義方法 取得名字信息public int getAge();//定義方法 取得年齡信息 } class petShop {//定義寵物商店private Link pets = new Link();//鏈表保存寵物信息public void add(Pet pet) {this.pets.add(pet);//向鏈表保存數(shù)據(jù)}public void delete(Pet pet) {this.pets.remove(pet);//從鏈表中刪除寵物信息}public Link search(String keyWord) {Link result = new Link();//保存結(jié)果//將結(jié)果合變?yōu)閷ο髷?shù)組的形式返回,因為集合保存的是Objective//但是真正要查詢的數(shù)據(jù)在pet接口對象的getName()方法的返回值Object obj[] = this.pets.toArray();for (int x = 0; x < obj.length; x++) {Pet p = (Pet) obj[x];//向下轉(zhuǎn)型if (p.getName().contains(keyWord)) {//查詢到結(jié)果result.add(p);//保存結(jié)果}}return result;} } class Cat implements Pet{private String name;private int age;public Cat(String name,int age){this.name = name;this.age = age;}public boolean equals(Object obj){if (this == obj){return true;}if (this == null){return false;}if (!(obj instanceof Cat)){return false;}Cat c = (Cat) obj;if(this.name.equals(c.name)&&this.age == c.age){return true;}return false;}public String getName(){//覆寫return this.name;}public int getAge(){return this.age;}public String toString(){//覆寫Objective類中的toStringreturn "貓的名字:"+this.name+"年齡:"+this.age;} } class Dog implements Pet{private String name;private int age;public Dog(String name,int age){this.name = name;this.age = age;}public boolean equals(Object obj){if (this == obj){return true;}if (this == null){return false;}if (!(obj instanceof Dog)){return false;}Dog d = (Dog) obj;if(this.name.equals(d.name)&&this.age == d.age){return true;}return false;}public String getName(){//覆寫return this.name;}public int getAge(){return this.age;}public String toString(){//覆寫Objective類中的toStringreturn "狗的名字:"+this.name+"年齡:"+this.age;}} public class Main {public static void main(String[] args) {petShop shop = new petShop();shop.add(new Cat("一號貓", 1));shop.add(new Cat("二號貓", 2));shop.add(new Cat("三號貓", 3));shop.add(new Cat("四號貓", 4));shop.add(new Dog("一號狗", 1));shop.add(new Dog("二號狗", 1));shop.add(new Dog("三號狗", 1));shop.add(new Dog("四號狗", 1));Link all = shop.search("");//輸入查詢的關(guān)鍵字Object obj[] = all.toArray();for (int x = 0; x < obj.length; x++) {System.out.println(obj[x]);}} }運行結(jié)果:
?基本的接口思路,不難,但是很重要
總結(jié)
以上是生活随笔為你收集整理的宠物商店 - MLDN 李兴华老师的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: android textwatcher
 - 下一篇: 工作278:控制数据从字典表获取