概述
//利用内部类对链表进行开发
classLink{private classNode{privateObject data;privateNode next;publicNode(Object data){this.data =data;
}//==========================1.数据增加方法====================================
public voidaddNode(Node newNode){if(this.next == null){ //1.this = this.root 2.this = this.root.next;...
this.next =newNode;
}else{this.next.addNode(newNode); //递归调用
}
}//==========================4.判断某个内容是否包含在节点之中====================================
public booleancontainsNode(Object data){if(data.equals(this.data)){return true;
}else{if(this.next != null){return this.next.containsNode(data);
}else{return false;
}
}
}//==========================5.根据索引取得数据====================================
public Object getNode(intindex){if(Link.this.foot++ ==index){return this.data;
}else{return this.next.getNode(index);
}
}//==========================6.根据索引替换内容====================================
public void setNode(intindex,Object data){if(Link.this.foot++ ==index){this.data =data;
}else{this.next.setNode(index,data);
}
}//==========================7.删除指定的数据====================================
public void removeNode(Node preNode,Object data){//preNode表示当前节点的前一个节点
if(data.equals(this.data)){
preNode.next= this.next;//当前的节点的上一个
}else{this.next.removeNode(this,data);
}
}//==========================8.将链表变为数组====================================
public voidtoArrayNode(){
Link.this.retArray[Link.this.foot++] = this.data;if(this.next != null){this.next.toArrayNode();
}
}
}//==========================上面显示的是内部类====================================
privateNode root;private int count = 0;//表示链表的长度
private int foot =0;//节点的脚标
private Object[] retArray;//表示返回的数组//==========================1.数据增加方法====================================
public voidadd(Object data){
Node newNode= newNode(data);if(this.root == null){ //表示根节点对象是一个空对象
this.root = newNode; //实例化根节点对象
}else{ //根节点对象已经实例化
this.root.addNode(newNode);
}if(data != null){this.count++;//每一次调用数据count自增一次
}
}//==========================2.取得链表长度====================================
public intsize(){returncount;
}//==========================3.判断链表是否是空链表====================================
public booleanisEmpty(){return this.count==0;
}//==========================4.判断某个内容是否包含在节点之中====================================
public booleancontains(Object data){if(this.root == null||data == null){return false;
}else{return this.root.containsNode(data);
}
}//==========================5.根据索引取得数据====================================
public Object get(intindex){this.foot = 0;if(this.count <=index){return null;
}else{return this.root.getNode(index);
}
}//==========================6.根据索引替换内容====================================
public void set(intindex,Object data){this.foot = 0;//重新设置脚标内容
if(this.count <=index){return ;//结束方法的调用
}else{this.root.setNode(index,data);
}
}//==========================7.删除指定的数据====================================
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--;
}
}//==========================8.将链表变为数组====================================
publicObject [] toArray(){if(this.root == null){return null;
}else{this.foot = 0;this.retArray = new Object[this.count];this.root.toArrayNode();return this.retArray;
}
}
}interface Pet{//定义宠物的标准
publicString getName();public intgetAge();
}class PetShop{//宠物商店
private Link pets = new Link();//保存多个宠物
public voidadd(Pet p){this.pets.add(p);//增加宠物信息
}public voiddelete(Pet p){this.pets.remove(p);//删除宠物信息
}//进行宠物的模糊查询,查询返回的是一个或者多个宠物信息
publicLink search(String keyWord){
Link result= new Link(); //将查询结果,通过链表进行保存
Object obj[] = this.pets.toArray(); //将宠物信息保存为对象数组
for(int x = 0;x
Pet p=(Pet)obj[x] ;if(p.getName().contains(keyWord)){ //此处调用String类中的 public boolean contains(String str);方法
result.add(p);
}
}returnresult;
}
}class Cat implementsPet{privateString name;private intage ;public Cat(String name,intage){this.name =name;this.age =age ;
}publicString getName(){return this.name;
}public intgetAge(){return this.age ;
}public booleanequals(Object obj){if(obj == null){return false;
}if(this ==obj){return true;
}if(!(obj instanceofCat)){return false;
}
Cat c=(Cat)obj;if(this.name.equals(c.name) && this.age ==c.age){return true;
}return false;
}publicString toString(){return "猫的名字:" + this.name + ",年龄" + this.age ;
}
}class Dog implementsPet{privateString name;private intage ;public Dog(String name,intage){this.name =name;this.age =age ;
}publicString getName(){return this.name;
}public intgetAge(){return this.age ;
}public booleanequals(Object obj){if(obj == null){return false;
}if(this ==obj){return true;
}if(!(obj instanceofDog)){return false;
}
Dog c=(Dog)obj;if(this.name.equals(c.name) && this.age ==c.age){return true;
}return false;
}publicString toString(){return "猫的名字:" + this.name + ",年龄" + this.age ;
}
}public classTest{public static voidmain(String args[]){
PetShop ps= newPetShop();
ps.add(new Cat("花猫",2));
ps.add(new Cat("土猫",4));
ps.add(new Cat("小猫",3));
ps.add(new Dog("土狗",5));
ps.add(new Dog("花狗",2));
ps.add(new Dog("小狗",6));
Link all= ps.search("小");
Object obj []=all.toArray();for(int x=0;x
System.out.println(obj[x]);
}
}
}
最后
以上就是秀丽宝贝为你收集整理的java 中的object_java中的Object类的全部内容,希望文章能够帮你解决java 中的object_java中的Object类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复