用Java创建一个猫及狗对象_Java面向对象编程(宠物商店)

public interface ILink { //设置泛型

public void add(E e); //添加数据

public int size(); //获取数据的个数

public boolean isEmpty(); //判断是否空集合

public Object[] toArray(); //将集合元素以数组的形式返回

public E get(int index); //根据索引获取数据

public void set(int index, E data); //修改索引数据

public boolean contains(E data); //判断数据是否存在

}/***@authorGu

* @cracked 2020-03-25 9:24*/

class LinkImpl implements ILink { //负责链表的操作//将Node定义为内部类,表示Node类只为Link类

private class Node { //负责数据与节点关系的匹配

private Object data; //保存节点的数据

private Node next; //保存下一个节点

publicNode(Object data) {this.data =data;

}//第1次调用:this = Link.root//第2次调用:this = Link.root.next;//第3次调用:this = Link.root.next;

public void addNode(Node newNode) { //处理节点关系

if(this.next == null) { //当前节点下一个为空

this.next =newNode;

}else{ //现在当前节点的下一个不为空

this.next.addNode(newNode);

}

}//第一次调用:this = Link.root//第一次调用:this = Link.root.next

public voidtoArrayNode() {

LinkImpl.this.retData[LinkImpl.this.foot++] = this.data;if(this.next != null){ //现在还有下一个节点

this.next.toArrayNode();

}

}//第一次调用:this = Link.root//第一次调用:this = Link.root.next

public booleancontainsNode(Object search) {if(search.equals(this.data)){ //找到了

return true;

}else{if(this.next != null) { //当前节点之后

return this.next.containsNode(search);

}else { //没有节点

return false;

}

}

}//第一次调用: this = Link.root//第一次调用: this = Link.root.next

public Object getNode(intindex){if(LinkImpl.this.foot++ ==index) {return this.data;

}else{this.next.getNode(index);

}return null;

}public void setNode(intindex,Object newData) {if(LinkImpl.this.foot++ == index) { //索引相同

this.data =newData;return ; //结束

}else{if(this.next != null) {this.next.setNode(index,newData);

}

}

}//第一次调用:this = Link.root.next,previous = Link.root;//第二次调用:this = Link.root.next.next,previous = Link.root.next;

public voidremoveNode(Node previous,Object data) {if(this.data.equals(data)) { //当前节点为要删除节点

previous.next = this.next; //删除当前了

}else{this.next.removeNode(this,data);

}

}

}//----------------以下为link类------------// private Object[] retData; //返回类型

private int foot = 0; //操作脚标

private int count = 0; //当前的保存个数

private Node root; //属于根节点,没有根节点就无法进行数据的保存

public voidadd(Object data) {if(data == null) {//人为的追加了规定,不允许存放空值

return ; //方法结束调用

}//如果要想进行数据的保存,那么就必须将数据封装在Node节点类里面//如果没有封装,则无法确认好节点的先后顺序

Node newNode = newNode(data);if(this.root == null) { //当前并没有根节点

this.root = newNode; //第一个节点设置为根节点

}else{ //根节点已经存在了//应该把此时的节点顺序的处理交给Node类自己完成

this.root.addNode(newNode);

}

count++;

}public int size() { //取得元素个数

return this.count;

}public booleanisEmpty() {return this.root == null && this.count == 0;

}public booleancontains(Object search) {//没有要查询的内容以及链表为空

if(search == null || this.root == null) {return false;

}return this.root.containsNode(search);

}publicObject[] toArray() {if(this.count == 0) {return null;

}//现在链表中存在有数据,则开辟指定长度的数组//该数组一定要交给Node类进行处理

this.retData = new Object[this.count];this.foot = 0; //进行清零的处理,需要进行脚标操作

this.root.toArrayNode(); //将数据的取出处理交给Node类完成

return this.retData;

}public void set(intindex,Object newData) {if(index >= this.count){ //超过了保存的个数

return ; //结束方法调用

}this.foot = 0;this.root.setNode(index,newData);

}public E get(intindex) {if(index >= this.count){ //超过了保存的个数

return null;

}this.foot = 0;return (E) this.root.getNode(index);

}public voidremove(Object data) {if(this.contains(data)){ //如果该数据存在则进行删除处理//首先需要判断要删除的是否为根节点数据

if(this.root.data.equals(data)) { //首先需要判断

this.root = this.root.next; //根节点变为下一个节点

}else { //不是根节点

this.root.next.removeNode(this.root,data);

}this.count--;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值