/**
*宠物就是一个标准,包含多类宠物
*定义宠物标准接口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就会按照覆写的方法执行
}
}
}

Java 实践的更多相关文章

  1. 系统间通信(5)——IO通信模型和JAVA实践 下篇

    7.异步IO 上面两篇文章中,我们分别讲解了阻塞式同步IO.非阻塞式同步IO.多路复用IO 这三种IO模型,以及JAVA对于这三种IO模型的支持.重点说明了IO模型是由操作系统提供支持,且这三种IO模 ...

  2. Redis与Java - 实践

    Redis与Java - 实践 标签 : Java与NoSQL Transaction Redis事务(transaction)是一组命令的集合,同命令一样也是Redis的最小执行单位, Redis保 ...

  3. 大文件拆分问题的java实践(附源码)

    引子 大文件拆分问题涉及到io处理.并发编程.生产者/消费者模式的理解,是一个很好的综合应用场景,为此,花点时间做一些实践,对相关的知识做一次梳理和集成,总结一些共性的处理方案和思路,以供后续工作中借 ...

  4. 大文件拆分方案的java实践(附源码)

    引子 大文件拆分问题涉及到io处理.并发编程.生产者/消费者模式的理解,是一个很好的综合应用场景,为此,花点时间做一些实践,对相关的知识做一次梳理和集成,总结一些共性的处理方案和思路,以供后续工作中借 ...

  5. 转: 低延迟系统的Java实践

    from:  http://blog.csdn.net/jacktan/article/details/41177779 在很久很久以前,如果有人让我用Java语言开发一个低延迟系统,我肯定会用迷茫的 ...

  6. 系统间通信(4)——IO通信模型和JAVA实践 中篇

    4.多路复用IO模型 在"上篇"文章中,我们已经提到了使用多线程解决高并发场景的问题所在,这篇文章我们开始 4-1.现实场景 我们试想一下这样的现实场景: 一个餐厅同时有100位客 ...

  7. 系统间通信(3)——IO通信模型和JAVA实践 上篇

    来源:http://blog.csdn.net/yinwenjie 1.全文提要 系统间通信本来是一个很大的概念,我们首先重通信模型开始讲解.在理解了四种通信模型的工作特点和区别后,对于我们后文介绍搭 ...

  8. 算法 《秦九韶算法java实践》

    [历史背景] 秦九韶算法是中国南宋时期的数学家秦九韶表述求解一元高次多项式的值的算法--正负开方术.它也能够配合牛顿法用来求解一元高次多项式的根.在西方被称作霍纳算法(Horner algorithm ...

  9. 算法 《霍纳的方法java实践》

    [历史背景] 霍纳的方法是中国南宋时期的数学家秦九韶表述求解一元高次多项式的值的算法--正负开方术. 它也能够配合牛顿法用来求解一元高次多项式的根.在西方被称作霍纳算法(Horner algorith ...

  10. Java实践 — SSH远程执行Shell脚本(转)

    原文地址:http://www.open-open.com/lib/view/open1384351384024.html 1. SSH简介         SSH是Secure Shell的缩写,一 ...

随机推荐

  1. EZOJ #82

    传送门 分析 首先我们发现$k$位数实际就是一位的情况的$k$次方 考虑一开始的总方案数是$2^{nm}$ 我们每一次枚举其中有$i$行$j$列 对于这种情况的容斥系数为$(-1)^{i+j}$ 方案 ...

  2. 最新解决VS2017+ Mysql + EF 创建实体数据模型 闪退的办法

    研究下来,就是最新的版本兼容性不好啊. 1.找到MySql管网,下载历史版本: mysql-connector-net-6.9.12 mysql-for-visualstudio-1.2.8 2.Nu ...

  3. [转]oracle11g 修改字符集 修改为ZHS16GBK

    转至:http://www.cnblogs.com/jay-xu33/p/5210098.html sqlplus /nolog conn /as sysdba shutdown immediate; ...

  4. Arcgis android 10.2安装方法

    请仔细对照博文做!!! 将arcgis android 10.2的压缩包解压 arcgis android 10.2下载地址http://pan.baidu.com/s/1sj2LKO9 Help-& ...

  5. C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值

    接收拦截+发送消息 对于处理所有消息.net 提供了wndproc进行重写 WndProc(ref Message m)protected override void WndProc(ref Mess ...

  6. c#处理未捕获的异常(UnhandledException)

    处理未捕获的异常,放在program类的Main函数下 1.UnhandledException 作用:接收未捕获到的异常 例: static void Main(string[] args) { A ...

  7. 微信开放平台 redirect_uri参数错误

    微信开放平台 redirect_uri参数错误   请注意是开放平台开放平台,公众平台和开放平台不是同一个. 解决办法 在写 授权回调域 时,地址只用写到域名级,不能写到域名下一级,这和QQ互联的回调 ...

  8. 使用穷人版profiler定位调试MySQL

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 周末闲得蛋疼,来英飞特做人工空气净化器.开了电脑后,习惯性得点击xshell按钮,进入InnoSQL稳定性测 ...

  9. 最大k乘积

    思路:看到这道题,第一思路就要是动态规划,不要想着用啥暴力或者排列组合,只会搞得很复杂. 动态规划的思路是对这个整数,我们从后向前进行划分k个数字,我们知道对于划分后的最后一个整数,它的位数要保证前面 ...

  10. Mysql union

    union简单来说就是多表链接,主要是用于(模糊)查询,全库搜索 多表搜索需要先将需要查询的表用union连接,然后在每一个union后面添加上相同的where条件 菜鸟教程