日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

宠物商店 - MLDN 李兴华老师

發布時間:2023/12/9 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 宠物商店 - MLDN 李兴华老师 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

根據視頻整理

?

class Link {class Node {private Node next;// 下一個節點private Object data;// 數據public Node(Object data) {this.data = data;}// 1.添加節點public void addNode(Node newNode) {if (this.next == null) {this.next = newNode;} else {this.next.addNode(newNode);}}// 2.數據查詢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.修改數據public void setNode(int index, Object data) {if (Link.this.foot++ == index) {this.data = data;}this.next.setNode(index, data);}// 4.獲取數據public Object getData(int index) {if (Link.this.foot++ == index) {return this.data;}return this.next.getData(index);}// 5.刪除數據public void removeNode(Node previous, Object data) {if (data.equals(this.data)) {previous.next = this.next;} else {this.next.removeNode(this, data);}}// 6.對象數組public void toArrayNode() {Link.this.retArray[Link.this.foot++] = this.data;if (this.next != null) {this.next.toArrayNode();}}}// ===================以上是內部類=========================private Node root;// 根節點private int count;// 計數器private int foot;// 腳標private Object[] retArray;// 對象數組// 1.添加數據public void add(Object data) {if (data == null) {return;}Node newNode = new Node(data);// 數據裝包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.修改數據public void set(int index, Object data) {if (index > this.count) {return;}this.foot = 0;this.root.setNode(index, data);}// 6.獲取數據public Object get(int index) {if (index > this.count) {return null;}this.foot = 0;return this.root.getData(index);}// 7.刪除數據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.對象數組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);//向鏈表保存數據}public void delete(Pet pet) {this.pets.remove(pet);//從鏈表中刪除寵物信息}public Link search(String keyWord) {Link result = new Link();//保存結果//將結果合變為對象數組的形式返回,因為集合保存的是Objective//但是真正要查詢的數據在pet接口對象的getName()方法的返回值Object obj[] = this.pets.toArray();for (int x = 0; x < obj.length; x++) {Pet p = (Pet) obj[x];//向下轉型if (p.getName().contains(keyWord)) {//查詢到結果result.add(p);//保存結果}}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("");//輸入查詢的關鍵字Object obj[] = all.toArray();for (int x = 0; x < obj.length; x++) {System.out.println(obj[x]);}} }

運行結果:

?基本的接口思路,不難,但是很重要

總結

以上是生活随笔為你收集整理的宠物商店 - MLDN 李兴华老师的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。