Java Object类

Java-Object类

在于统一对象、数组、抽象、接口

Object类的基本作用:

         Object时所有类的父类,任何一个类在定义的时候没有明确的继承一个父类,那么它就是object类的子类;即:class Book {}  ==  class Book extends Object {} 定义作用是一样的

         在Java中,类的继承关系一直存在(除了Object类不存在继承关系)即:所有类都有继承一个父类;故——最大的好处:可以利用Object类可以接收全部类的对象(因为可以向上自动转型);于是在不确定参数类型(对象接收)的时候,使用Object类接收对象是最好的选择。

class Book {   // class Book extends Object { }

}

public class Demo {

         public static void main(String [] args) {

                   Object obja = new Book() ;    //向上转型

                   Object objb = "hello" ;   //向上转型

                   Book b = (Book) objb ; //向下转型

         }

}

         Object类中,有一个无参构造方法—(子类被实例化的时候,子类构造方法一定会默认调用父类的无参构造)

         严格意义一般不遵守),任何一个简单Java类,都应该覆写Object类的三个方法:

1、 public String toString() 取得对象信息

2、 public boolean equals(Object obj) :对象比较

3、 public int hashCode() :取得对象哈希(HASH)码

toString() 方法:

         通过代码,直接输出对象与调用toString() 方法后输出对象的功能(结果)是完全一样的;在进行对象输出的时候,输出操作会自动调用对象中的toString()方法,将对象变为字符串后再输出,而默认情况下,Object类中的toString() 方法为了适应所有对象的输出,所以只输出了对象的编码。如果需求有变化,也可以覆写toString() 方法

class Book {  // class Book extends Object { }

         private String title ;

         private double price ;

         public Book(String title , double price) {

                   this.title = title ;

                   this.price = price ;

         }

         public String toString() {

                   return this.title + this.price ;

         }

}

public class Demo {

         public static void main(String [] args) {

                   Book b = new Book("Java",11.1) ;

                   System.out.println(b) ;

         }

}

         上例代码:通过覆写toString() 方法,使得直接输出对象时调用toString()方法直接输出结果,等同于缩少当前代码量。

equals() 对象比较方法:

         对象比较是一个核心概念;使用equals()完成对象比较:

class Book {  // class Book extends Object { }

         private String title ;

         private double price ;

         public Book(String title , double price) {

                   this.title = title ;

                   this.price = price ;

         }

    public boolean equals(Object obj) {

        if ( this == obj ) { //地址相同

            return true ;

        }

        if ( obj == null ) {

            return false ;

        }

        if ( !(obj instanceof Book) ) {  // 不是本类实例

           return false ;

        }

        // 由于比较子类的属性,而Object父类看不见,故此需要向下转型

        Book book = (Book) obj ;

        if ( this.title.equals(book.title)

             && this.price == book.price ) {

            return true ;

        }

        return false ;

    }

         public String toString() {

                   return this.title + this.price ;

         }

}

public class Demo {

         public static void main(String [] args) {

                   Book b1 = new Book("Java",11.1) ;

                   Book b2 = new Book("Java Web",21.1) ;

                   System.out.println(b1.equals(b2));

         }

}

Object类可以接收一切引用类型

         Object类是所有类的父类,所以Object类的对象可以接收所有类的对象;可以除了类的对象以外,Object类还可以接收数组和接口对象。

public class Demo {

         public static void main(String [] args) {

                   Object  obj = new int [] {1,2,3} ;  // 向上转型

                   if ( obj instanceof int [] ) {  // 数组与object之间的接收

                            int data [] = (int[])  obj ; // 向下转型

                            for ( int x = 0 ; x < data.length ; x ++ ) {

                                     System.out.println(data[x]) ;

                            }

                   }

         }

}

interface A {

         public void fun() ;

}

class B implements A {

         public void fun() {

                   System.out.println("Hello,World!") ;

         }

}

public class Demo {

         public static void main(String [] args) {

       A a = new B() ;      //接口对象

       Object obj = a ;  //接收接口

       A t = (A) obj ;      //向下转型

                   t.fun() ;    

         }

}

         Java程序的参数,都会统一再Object类上。

链表:

         因为每一次使用链表都进行重复的开发,但是Object可以接收任何类型,同时再链表中由于需要对象比较的功能,Object中提供了equals()方法

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 ;

                  

         }

}

 

public class Demo {

         public static void main(String [] args) {

                   Link all = new Link() ;   //实例化

                   all.add("A") ;

                   all.add("B") ;

                   all.add("C") ;

 

                   all.remove("A") ;

                   Object data [] = all.toArray() ; 

                   for ( int x = 0 ; x < data.length ; x ++ ) {

                            String str = (String) data[x] ;  //向下转型

// 由于再存放数据时,都是以Object类型存放的,所以再读取数据的时候向下转型为String类型

                            System.out.println(str) ;

                   }

         }

}
应用链表代码分析
原文地址:https://www.cnblogs.com/wangyuyang1016/p/10795286.html