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的缩写,一 ...
随机推荐
- Luogu 2151 [SDOI2009]HH去散步
BZOJ 1875 矩阵乘法加速递推. 如果不要求不能走同一条边,那么直接构造出矩阵快速幂即可,但是不走相同的道路,怎么办? 发现边数$m$也很小,我们直接把$2 * m$开成一个矩阵,相当于记录上一 ...
- About English Web Site Font
Which font will you choose when develop one website! Actually , I have no idea about this! If you ha ...
- Linux sogou input method
afda@afda-Y720-15IKB:~$ wget "http://pinyin.sogou.com/linux/download.php?f=linux&bit=64&quo ...
- 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(一)
1.ViewPager实现Tab 首先实现底部和底部布局 <?xml version="1.0" encoding="utf-8"?> <Li ...
- delphi xe6 调用java GPS的方法
如果用xe6自带的LocationSensor控件,默认优先使用网络位置,网络位置定位精度不准确,不能满足高精度定位的要求.但xe6自带的LocationSensor控件不能指定网络定位优先还是GPS ...
- SingleClass单例类
前言 对于一个单例类,无论初始化单例对象多少次,在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死,该实例对象就不会被释放,并且该对象是全局的,能够被整个系统访问到. 在应用这个模 ...
- hdu4622(后缀自动机模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意: 先输入一个长度为 n 的字符串, 然后有 q 个形如 l, r 的询问, 对于每个询问 ...
- ConcurrentHashMap原理详解
参考链接:https://www.cnblogs.com/chengxiao/p/6842045.html https://www.cnblogs.com/ITtangtang/p/3948786.h ...
- Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- 数据结构11: 栈(Stack)的概念和应用及C语言实现
栈,线性表的一种特殊的存储结构.与学习过的线性表的不同之处在于栈只能从表的固定一端对数据进行插入和删除操作,另一端是封死的. 图1 栈结构示意图 由于栈只有一边开口存取数据,称开口的那一端为“栈顶”, ...