Java 实践
/**
*宠物就是一个标准,包含多类宠物
*定义宠物标准接口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 实践的更多相关文章
- 系统间通信(5)——IO通信模型和JAVA实践 下篇
7.异步IO 上面两篇文章中,我们分别讲解了阻塞式同步IO.非阻塞式同步IO.多路复用IO 这三种IO模型,以及JAVA对于这三种IO模型的支持.重点说明了IO模型是由操作系统提供支持,且这三种IO模 ...
- Redis与Java - 实践
Redis与Java - 实践 标签 : Java与NoSQL Transaction Redis事务(transaction)是一组命令的集合,同命令一样也是Redis的最小执行单位, Redis保 ...
- 大文件拆分问题的java实践(附源码)
引子 大文件拆分问题涉及到io处理.并发编程.生产者/消费者模式的理解,是一个很好的综合应用场景,为此,花点时间做一些实践,对相关的知识做一次梳理和集成,总结一些共性的处理方案和思路,以供后续工作中借 ...
- 大文件拆分方案的java实践(附源码)
引子 大文件拆分问题涉及到io处理.并发编程.生产者/消费者模式的理解,是一个很好的综合应用场景,为此,花点时间做一些实践,对相关的知识做一次梳理和集成,总结一些共性的处理方案和思路,以供后续工作中借 ...
- 转: 低延迟系统的Java实践
from: http://blog.csdn.net/jacktan/article/details/41177779 在很久很久以前,如果有人让我用Java语言开发一个低延迟系统,我肯定会用迷茫的 ...
- 系统间通信(4)——IO通信模型和JAVA实践 中篇
4.多路复用IO模型 在"上篇"文章中,我们已经提到了使用多线程解决高并发场景的问题所在,这篇文章我们开始 4-1.现实场景 我们试想一下这样的现实场景: 一个餐厅同时有100位客 ...
- 系统间通信(3)——IO通信模型和JAVA实践 上篇
来源:http://blog.csdn.net/yinwenjie 1.全文提要 系统间通信本来是一个很大的概念,我们首先重通信模型开始讲解.在理解了四种通信模型的工作特点和区别后,对于我们后文介绍搭 ...
- 算法 《秦九韶算法java实践》
[历史背景] 秦九韶算法是中国南宋时期的数学家秦九韶表述求解一元高次多项式的值的算法--正负开方术.它也能够配合牛顿法用来求解一元高次多项式的根.在西方被称作霍纳算法(Horner algorithm ...
- 算法 《霍纳的方法java实践》
[历史背景] 霍纳的方法是中国南宋时期的数学家秦九韶表述求解一元高次多项式的值的算法--正负开方术. 它也能够配合牛顿法用来求解一元高次多项式的根.在西方被称作霍纳算法(Horner algorith ...
- Java实践 — SSH远程执行Shell脚本(转)
原文地址:http://www.open-open.com/lib/view/open1384351384024.html 1. SSH简介 SSH是Secure Shell的缩写,一 ...
随机推荐
- p4301 [CQOI2013]新Nim游戏
传送门 分析 通过nim游戏我们可以知道我们现在的任务就是通过两轮之后使得剩余的几堆异或和为非0数 所以我们只需要在第一步使得剩余集合的任意非空子集的异或和非0即可 于是我们考虑线性基 我们知道线性基 ...
- Log4Net 调试时输出sql到 视图->输出的sql语句
1.log4net.xml <?xml version="1.0" encoding="utf-8" ?> <!--log4net 配置--& ...
- 树莓派研究笔记(3)-- 安装VNC
小屏幕太小了,眼睛快看瞎了,必须安装VNC 才行啊. 更新—2018-02-04 最新版本的系统中自带了VNC了,只需要在 菜单 Preferences -> Raspberry Pi Conf ...
- 使用Elasticsearch-jdbc为MySQL数据库建立索引
elasticsearch-jdbc 环境 Ubuntu 14.04 JDK 1.8.0_66 Elasticsearch 2.3.1 Elasticsearch-jdbc 2.3.1.0 Elast ...
- Java50道经典习题-程序50 文件IO
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中. impo ...
- MongoDB 分片2
mongodb 在windows下面进行分片 mongodb 更新很快,在做分片的时候,查找了不少文章,但是很多已经过时了.现在把我搭建的过程及命令分享给大家.我用的是最新版本windows版3.4. ...
- vue 用axios实现调用接口下载excel
了解的方式有两种: 1. 用a标签,href设置为后端提供的excel接口 <a href="excel接口">导出</a> 简单方便,缺点就是当有toke ...
- day-15递归与函数
生成器send方法 send的工作原理 1.send发生信息给当前停止的yield 2.再去调用__next__()方法,生成器接着往下指向,返回下一个yield值并停止 # 案例: persons ...
- asp web.config文件里compilation的assemblies add的元素来自哪里
该程序集组合由版本.区域性和公钥标记组成. ASP.NET 首先在应用程序的专用 Bin 目录中搜索程序集 DLL,然后在系统程序集缓存中搜索程序集 DLL. add 元素添加要在动态资源编译期间使用 ...
- C#调用WCF问题汇总
1.添加服务后无法引用,添加服务时打开高级,将下面的选项去掉就可以引用了. 2.在程序中引用后,运行时报“找不到引用协定‘’的默认终结点元素”错误,这是由于在工程的App.config里没有生成配置节 ...