/**
*宠物就是一个标准,包含多类宠物
*定义宠物标准接口Pet
*定义Cat和Dog两个Pet接口的子类
*使用链表结构动态存储宠物信息
*定义商店类(工厂类),负责宠物的上架(链表添加)、下架(链表删除)、查找(模糊查找)
*
*所有数据类型均为 接口————Pet
*
*
*/
class Link { //链表类(外部可调用) class Node { //节点类,定义该内部类,只可服务于Link类 private Object data ; //节点数据 private Node next ; //引用关系(顺序) public Node(Object data) { this.data = data ; } public void addNode(Node newNode){ if ( this.next == null){ //下一个节点为空则设置下一个节点 this.next = newNode ; } else { //若节点存在,则向后移 this.next.addNode(newNode) ; } } public boolean containsNode(Object data) { //(如果比较的数据是对象,则定义一个对象比较的方法。) if ( data.equals(this.data)) { //对比data return true ; } else { if ( this.next != null ) { // 对比下一个data return this.next.containsNode(data) ; } else { return false ; } } } public Object getNode(int index) { //索引查找 // 1 比较index和foot的值是否相等 // 2 将foot的内容自增 1 if ( Link.this.foot ++ == index) { //对比当前节点的foot return this.data ; } else { //下一个节点 return this.next.getNode(index) ; } } public void setNode(int index , Object data) { if ( Link.this.foot ++ == index ) { this.data = data ; //内容修改(替换 ) } else { if ( this.next != null ) { this.next.setNode(index,data) ; } } } // 要传递上一个节点以要删除的数据 public void removeNode( Node previous , Object data ) { if ( data.equals(this.data)){//数据满足删除条件 previous.next = this.next ; //上一个节点的next指向当前的next(空出当前节点) } else { // 继续匹配查询删除 if(this.next != null) { this.next.removeNode(this , data) ;//this表示当前对象,将this给previous // 因为previous接收的是上一个对象。 } } } public void toArrayNode() { Link.this.retArray[Link.this.foot ++] = this.data ; if ( this.next != null ) { this.next.toArrayNode() ; } } } /* ========================================================================== */ private Node root ; //根节点 private int count = 0 ; //保存元素的个数 private int foot = 0 ; //保存链表的索引编号 private Object [] retArray ; //对象数组 public void add(Object data) { //数据添加 Node newNode = new Node(data) ; if ( this.root == null ){//无根节点,保存根节点 this.root = newNode ; } else { //向Node调用 this.root.addNode(newNode) ; } this.count ++ ; //每一个保存后数据量自加 1 } public int size() { //判断返回链表数据数量 return this.count ; } public boolean isEmpty() { //判断链表是否为空 return this.count == 0 ; // 根据链表数量判断 } public boolean contains(Object data) { //查询数据是否存在 if ( data == null || this.root == null ) { //查找数据为空则false return false ; } return this.root.containsNode(data) ; //不为空则调用Node查找数据; } public Object get(int index) { //设置索引目标 index if ( index > this.count ) { return null ; } this.foot = 0 ; //索引归零,表示从左往右查找 return this.root.getNode(index) ; //调用getNode()查找 } public void set(int index , Object data) {//修改链表数据内容 if ( data == null || this.root == null ) { return ; } this.foot = 0 ; // 重置foot,便于查找内容 this.root.setNode(index,data) ; //调用Node修改 } /* 如果要删除的是根节点:root指向下一个节点即可 如果删除的不是根节点:删除的前一个节点,下一个节点指向删除的下一个节点 删除数据的形式:当前节点的上一个节点next = 当前节点的next 需要设置一个方法专门处理非根节点的删除(removeNode()方法) */ public void remove(Object data) { if ( this.contains(data)) {//contains() 判断数据是否存在 // 判断删除的数据是不是根节点数据 // root是Node类的对象,此处直接访问了Node对象的私有属性 data if ( data.equals(this.root.data)) { this.root = this.root.next ; //空出当前节点(改变根节点) } else { this.root.next.removeNode(this.root , data) ; } this.count -- ; //链表个数减一 } } // 首先开辟一个数组空间,空间 == count public Object [] toArray() { //将链表以对象数组形式返回 if (this.root == null ) { return null ; } this.foot = 0 ; //下标控制 this.retArray = new Object [this.count] ; //开辟一个数组 this.root.toArrayNode() ; //Node处理 return retArray ;
}
} interface Pet { // 宠物标准接口Pet
public String getName() ;//得到名字
public int getAge() ;//得到年龄
} class PetShop { // 商店
private Link pets = new Link() ;//保存的宠物信息 ** 利用链表存储结构来保存信息
public void add(Pet pet) {//上架宠物信息pet ** 调用链表add()来添加数据
this.pets.add(pet) ;//向链表中保存数据pet
}
public void delete(Pet pet) { // 下架
this.pets.remove(pet) ; //从链表中删除数据pet
}
public Link search(String keyWord) {
Link result = new Link() ;
// 将集合变为对象数组的形式返回,因为保存的是Object
// 但是真正要查询的数据在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 { // Cat子类 实现 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 (obj==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 toString() {
return "Name:" + this.name + "Age:" + this.age ;
} public String getName() {
return this.name ;
}
public int getAge() {
return this.age ;
}
} class Dog implements Pet { // Dog子类 实现 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 (obj==null) {
return false ;
}
if (!(obj instanceof Dog)) {
return false ;
}
Dog c = (Dog) obj ;
if ( this.name.equals(c.name) && this.age == c.age ) {
return true ;
}
return false ;
}
public String toString() {
return "Name:" + this.name + "Age:" + this.age ;
}
public String getName() {
return this.name ;
}
public int getAge() {
return this.age ;
}
} public class TestDemo {
public static void main(String [] args) {
PetShop shop = new PetShop() ;
shop.add(new Cat("Ha",1));
shop.add(new Cat("Ea",2));
shop.add(new Cat("Lw",3));
shop.add(new Cat("O",4));
shop.add(new Dog("Wa",5));
shop.add(new Dog("O",6));
shop.add(new Dog("Rw",7));
shop.add(new Dog("D",8)); Link all = shop.search("a") ;//模糊查询
Object obj [] = all.toArray() ;
for ( int x = 0 ; x < obj.length ; x++ ) {
System.out.println(obj[x]) ;
}
/**
*宠物就是一个标准,包含多类宠物
*定义宠物标准接口Pet
*定义Cat和Dog两个Pet接口的子类
*使用链表结构动态存储宠物信息(链表中存放的是对象地址)
*定义商店类(工厂类),负责宠物的上架(链表添加)、下架(链表删除)、查找(模糊查找)
*
*所有数据类型均为 接口————Pet
*
*/
class Link { //链表类(外部可调用) class Node { //节点类,定义该内部类,只可服务于Link类 private Object data ; //节点数据 private Node next ; //引用关系(顺序) public Node(Object data) { this.data = data ; } public void addNode(Node newNode){ if ( this.next == null){ //下一个节点为空则设置下一个节点 this.next = newNode ; } else { //若节点存在,则向后移 this.next.addNode(newNode) ; } } public boolean containsNode(Object data) { //(如果比较的数据是对象,则定义一个对象比较的方法。) if ( data.equals(this.data)) { //对比data return true ; } else { if ( this.next != null ) { // 对比下一个data return this.next.containsNode(data) ; } else { return false ; } } } public Object getNode(int index) { //索引查找 // 1 比较index和foot的值是否相等 // 2 将foot的内容自增 1 if ( Link.this.foot ++ == index) { //对比当前节点的foot return this.data ; } else { //下一个节点 return this.next.getNode(index) ; } } public void setNode(int index , Object data) { if ( Link.this.foot ++ == index ) { this.data = data ; //内容修改(替换 ) } else { if ( this.next != null ) { this.next.setNode(index,data) ; } } } // 要传递上一个节点以要删除的数据 public void removeNode( Node previous , Object data ) { if ( data.equals(this.data)){//数据满足删除条件 previous.next = this.next ; //上一个节点的next指向当前的next(空出当前节点) } else { // 继续匹配查询删除 if(this.next != null) { this.next.removeNode(this , data) ;//this表示当前对象,将this给previous // 因为previous接收的是上一个对象。 } } } public void toArrayNode() { Link.this.retArray[Link.this.foot ++] = this.data ; if ( this.next != null ) { this.next.toArrayNode() ; } } } /* ========================================================================== */ private Node root ; //根节点 private int count = 0 ; //保存元素的个数 private int foot = 0 ; //保存链表的索引编号 private Object [] retArray ; //对象数组 public void add(Object data) { //数据添加 Node newNode = new Node(data) ; if ( this.root == null ){//无根节点,保存根节点 this.root = newNode ; } else { //向Node调用 this.root.addNode(newNode) ; } this.count ++ ; //每一个保存后数据量自加 1 } public int size() { //判断返回链表数据数量 return this.count ; } public boolean isEmpty() { //判断链表是否为空 return this.count == 0 ; // 根据链表数量判断 } public boolean contains(Object data) { //查询数据是否存在 if ( data == null || this.root == null ) { //查找数据为空则false return false ; } return this.root.containsNode(data) ; //不为空则调用Node查找数据; } public Object get(int index) { //设置索引目标 index if ( index > this.count ) { return null ; } this.foot = 0 ; //索引归零,表示从左往右查找 return this.root.getNode(index) ; //调用getNode()查找 } public void set(int index , Object data) {//修改链表数据内容 if ( data == null || this.root == null ) { return ; } this.foot = 0 ; // 重置foot,便于查找内容 this.root.setNode(index,data) ; //调用Node修改 } /* 如果要删除的是根节点:root指向下一个节点即可 如果删除的不是根节点:删除的前一个节点,下一个节点指向删除的下一个节点 删除数据的形式:当前节点的上一个节点next = 当前节点的next 需要设置一个方法专门处理非根节点的删除(removeNode()方法) */ public void remove(Object data) { if ( this.contains(data)) {//contains() 判断数据是否存在 // 判断删除的数据是不是根节点数据 // root是Node类的对象,此处直接访问了Node对象的私有属性 data if ( data.equals(this.root.data)) { this.root = this.root.next ; //空出当前节点(改变根节点) } else { this.root.next.removeNode(this.root , data) ; } this.count -- ; //链表个数减一 } } // 首先开辟一个数组空间,空间 == count public Object [] toArray() { //将链表以对象数组形式返回 if (this.root == null ) { return null ; } this.foot = 0 ; //下标控制 this.retArray = new Object [this.count] ; //开辟一个数组 this.root.toArrayNode() ; //Node处理 return retArray ;
}
} interface Pet { // 宠物标准接口Pet
public String getName() ;//得到名字
public int getAge() ;//得到年龄
} class PetShop { // 商店
private Link pets = new Link() ;//保存的宠物信息 ** 利用链表存储结构来保存信息
public void add(Pet pet) {//上架宠物信息pet ** 调用链表add()来添加数据
this.pets.add(pet) ;//向链表中保存数据pet
}
public void delete(Pet pet) { // 下架
this.pets.remove(pet) ; //从链表中删除数据pet
}
public Link search(String keyWord) {
Link result = new Link() ;
// 将集合变为对象数组的形式返回,因为保存的是Object
// 但是真正要查询的数据在Pet接口对象的getName() 方法中
// 所以有必要向下转型
Object obj [] = this.pets.toArray() ;
// 核心思维:将查询条件与链表中的name做比较,若查到则将该链表的地址存入新的链表中
for ( int x = 0 ; x < obj.length ; x ++ ) {
Pet p = (Pet) obj[x] ; //向下转型
if ( p.getName().contains(keyWord)) { //查询到的 ** char.contains(str)方法:如果当前char中包含str则返回true
result.add(p) ; // 开辟新的链表 result;并将查到的符合条件的链表地址存入链表result
}
}
return result ; // 返回链表 result
}
} class Cat implements Pet { // Cat子类 实现 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 (obj==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 toString() {
return "Name:" + this.name + "Age:" + this.age + "Cat";
} public String getName() {
return this.name ;
}
public int getAge() {
return this.age ;
}
} class Dog implements Pet { // Dog子类 实现 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 (obj==null) {
return false ;
}
if (!(obj instanceof Dog)) {
return false ;
}
Dog c = (Dog) obj ;
if ( this.name.equals(c.name) && this.age == c.age ) {
return true ;
}
return false ;
}
public String toString() {
return "Name:" + this.name + "Age:" + this.age + "Dog";
}
public String getName() {
return this.name ;
}
public int getAge() {
return this.age ;
}
} public class TestDemo {
public static void main(String [] args) {
PetShop shop = new PetShop() ;
shop.add(new Cat("Ha",1));
shop.add(new Cat("Ea",2));
shop.add(new Cat("Lw",3));
shop.add(new Cat("O",4));
shop.add(new Dog("Wa",5));
shop.add(new Dog("O",6));
shop.add(new Dog("Rw",7));
shop.add(new Dog("D",8)); Link all = shop.search("a") ;//模糊查询 ** all接受shop.search()返回的链表对象
Object obj [] = all.toArray() ; //返回对象数组 ** 链表当中存放的是子类的堆内存地址(对象地址)
for ( int x = 0 ; x < obj.length ; x++ ) {
System.out.println(obj[x].toString()) ;
// 由于Object类的toString()方法特性,只要子类中定义了toSting就会按照覆写的方法执行
}
}
}

最新文章

  1. opencv用imread( argv[1], 1)读取图片
  2. Comparison method violates its general contract 关于jdk自带算法问题
  3. 【HDU 2089】不要62
  4. Elven Postman(BST )
  5. C语言 malloc、calloc、realloc的区别
  6. 面试知识点总结之Java语言
  7. Mybatis对MySQL中BLOB字段的读取
  8. NFS安装过程
  9. CRM SQL 共享
  10. STL容器的内存分配
  11. Windows下Apache+Django+mod_wsgi的static和media问题处理
  12. ECC椭圆曲线详解(有具体实例)
  13. Webpack 2 视频教程 019 - Webpack 2 中配置多页面编译
  14. iframe结构的项目,登录页面嵌套
  15. Visual Studio Code Shortcuts
  16. jquery.form插件 提交表单 type=&quot;hidden&quot;取不到值的问题记录
  17. Vue CLI 3+tinymce 5富文本编辑器整合
  18. Linux+Jenkins环境搭建
  19. 【mysql】group_concat函数substring_index函数
  20. springboot2.0 最大上传文件大小遇到的错误Failed to bind properties under &#39;spring.servlet.multipart.max-file-size&#39;

热门文章

  1. vue 之 nodejs中npm的使用
  2. JS 求解时间差
  3. 数据结构--树--AVL树
  4. NPOI读取Excel文件
  5. jQuery实例代码-表单相关
  6. 原子变量与CAS算法小结
  7. javascript javascript面向对象的理解及简单的示例
  8. 【转】Linux将composer的bin目录放到PATH环境变量中
  9. 解决Eclipse 启动后总是Building WorkSpace(sleeping) Java报错和处理
  10. Python学习过程(三)