java中的Object类

Object是所有类的父类,所有类都默认继承Object;Object中定义的方法:

取得对象信息:public String toString();

对象比较:public boolean equals(Obejct obj);

取得兑现哈希码:public int hashCode();

利用Object改善重现定义链表

之前链表点击:原始链表

//利用内部类对链表进行开发
class Link{
    private class Node{
        private Object data;
        private Node next;
        public Node(Object data){
            this.data = data;
        }
    //==========================1.数据增加方法====================================
        public void addNode(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 boolean containsNode(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(int index){
            if(Link.this.foot++ == index){
                return this.data;
            }else{
                return this.next.getNode(index);
            }
        }
    //==========================6.根据索引替换内容====================================
        public void setNode(int index,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 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;//表示返回的数组
    //==========================1.数据增加方法====================================
    public void add(Object data){
        Node newNode = new Node(data);
        if(this.root == null){        //表示根节点对象是一个空对象
            this.root = newNode;    //实例化根节点对象
        }else{    //根节点对象已经实例化
            this.root.addNode(newNode);
        }
        if(data != null){
            this.count++;//每一次调用数据count自增一次
        }
    }
    //==========================2.取得链表长度====================================
    public int size(){
        return count;
    }
    //==========================3.判断链表是否是空链表====================================
    public boolean isEmpty(){
        return this.count==0;
    }
    //==========================4.判断某个内容是否包含在节点之中====================================
    public boolean contains(Object data){
        if(this.root == null||data == null){
            return false;
        }else{
            return this.root.containsNode(data);
        }
    }
    //==========================5.根据索引取得数据====================================
    public Object get(int index){
        this.foot = 0;
        if(this.count <= index){
            return null;
        }else{
            return this.root.getNode(index);
        }
    }
    //==========================6.根据索引替换内容====================================
    public void set(int index,Object data){
        this.foot = 0;//重新设置脚标内容
        if(this.count  <= index){
            return ;//结束方法的调用
        }else{
            this.root.setNode(index,data);
        }
    }
    //==========================7.删除指定的数据====================================
    public void remove(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.将链表变为数组====================================
    public Object [] toArray(){
        if(this.root == null){
            return null;
        }else{
            this.foot = 0;
            this.retArray = new Object[this.count];
            this.root.toArrayNode();
            return this.retArray;
        }
    }
}
public class Test{
    public static void main(String args[]){
       Link all = new Link();
       all.add("a");
       all.add("b");
       all.add("c");
       all.add("d");
       all.remove("a");
       Object obj[] = all.toArray();
       for(int x=0;x<obj.length;x++){
            String str = (String)obj[x];
            System.out.print(str+"	");
       }
        
    }
}

 利用链表,object 、接口等知识,定义一个简单的宠物商店

//利用内部类对链表进行开发
class Link{
    private class Node{
        private Object data;
        private Node next;
        public Node(Object data){
            this.data = data;
        }
    //==========================1.数据增加方法====================================
        public void addNode(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 boolean containsNode(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(int index){
            if(Link.this.foot++ == index){
                return this.data;
            }else{
                return this.next.getNode(index);
            }
        }
    //==========================6.根据索引替换内容====================================
        public void setNode(int index,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 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;//表示返回的数组
    //==========================1.数据增加方法====================================
    public void add(Object data){
        Node newNode = new Node(data);
        if(this.root == null){        //表示根节点对象是一个空对象
            this.root = newNode;    //实例化根节点对象
        }else{    //根节点对象已经实例化
            this.root.addNode(newNode);
        }
        if(data != null){
            this.count++;//每一次调用数据count自增一次
        }
    }
    //==========================2.取得链表长度====================================
    public int size(){
        return count;
    }
    //==========================3.判断链表是否是空链表====================================
    public boolean isEmpty(){
        return this.count==0;
    }
    //==========================4.判断某个内容是否包含在节点之中====================================
    public boolean contains(Object data){
        if(this.root == null||data == null){
            return false;
        }else{
            return this.root.containsNode(data);
        }
    }
    //==========================5.根据索引取得数据====================================
    public Object get(int index){
        this.foot = 0;
        if(this.count <= index){
            return null;
        }else{
            return this.root.getNode(index);
        }
    }
    //==========================6.根据索引替换内容====================================
    public void set(int index,Object data){
        this.foot = 0;//重新设置脚标内容
        if(this.count  <= index){
            return ;//结束方法的调用
        }else{
            this.root.setNode(index,data);
        }
    }
    //==========================7.删除指定的数据====================================
    public void remove(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.将链表变为数组====================================
    public Object [] 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{//定义宠物的标准
    public String getName();
    public int getAge();
}
class PetShop{//宠物商店
    private Link pets = new Link();//保存多个宠物
    public void add(Pet p){
        this.pets.add(p);//增加宠物信息
    }
    public void delete(Pet p){
        this.pets.remove(p);//删除宠物信息
    }
    //进行宠物的模糊查询,查询返回的是一个或者多个宠物信息
    public Link search(String keyWord){
        Link result = new Link();    //将查询结果,通过链表进行保存
        Object obj[] = this.pets.toArray();    //将宠物信息保存为对象数组
        for(int x = 0;x<obj.length;x++){
            Pet p = (Pet)obj[x] ;
            if(p.getName().contains(keyWord)){ //此处调用String类中的 public boolean contains(String str);方法
                result.add(p);
            }
        }
        return result;
    }
}
class Cat implements Pet{
    private String name;
    private int age ;
    public Cat(String name,int age){
        this.name = name;
        this.age = age ;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age ;
    }
    public boolean equals(Object obj){
        if(obj == null){
            return false;
        }
        if(this == obj){
            return true;
        }
        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 "猫的名字:" + this.name + ",年龄" + this.age  ;
    }
}
class Dog implements Pet{
    private String name;
    private int age ;
    public Dog(String name,int age){
        this.name = name;
        this.age = age ;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age ;
    }
    public boolean equals(Object obj){
        if(obj == null){
            return false;
        }
        if(this == obj){
            return true;
        }
        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 "猫的名字:" + this.name + ",年龄" + this.age  ;
    }
}
public class Test{
    public static void main(String args[]){
      PetShop ps = new PetShop();
      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<obj.length;x++){
        System.out.println(obj[x]);
      }
      
    }
}
原文地址:https://www.cnblogs.com/hu1056043921/p/7338028.html