Java learning summary: 15

Object class

Basic definition of Object class

The Object class is the parent class of all classes, which means that if any class does not explicitly inherit a parent class when it is defined, it is a subclass of the Object class.
Insert picture description here

Obtain object information: toString ()

In the design of the toString () method in the Object class, since it should be considered that it can meet the output information of all objects, the object code returned by default.

class Book1{	//此类为Object子类
    private String title;
    private double price;
    public Book1(String title,double price){
        this.title=title;
        this.price=price;
    }
    public String toString(){	//代替getInfo(),并且toString()可以自动调用
        return "书名:"+this.title+",价格:"+this.price;
    }
    //setter、getter、无参结构略
}
public class Test1_1_4_5 {
    public static void main(String args[]){
        Book1 b=new Book1("Java开发",79.9);		//实例化对象
        System.out.println(b);		//直接输出对象,默认调用toString()
    }
}

Object comparison: equals ()

Example: Comparing objects

class Book14{
    private String title;
    private double price;
    public Book14(String title,double price){
        this.title=title;
        this.price=price;
    }
    public boolean equals(Object obj){
        if (this==obj){	//地址相同
            return true;
        }
        if (obj==null){	//对象内容为null
            return false;
        }
        if (!(obj instanceof Book14)){	//不是本类实例
            return false;
        }
        Book14 book=(Book14)obj;
        if (this.title.equals(book.title)&&this.price==book.price){
            return true;
        }
        return false;
    }
    public String toString(){	//替代了getInfo(),并且toString()可以自动调用
        return "书名:"+this.title+",价格:"+this.price;
    }
    //setter、getter、无参结构略
}
public class Test1_1_4_6 {
    public static void main(String args[]){
        Book14 b1=new Book14("Java开发",79.9);	//实例化对象
        Book14 b2=new Book14("Java开发",79.9);
        System.out.println(b1.equals(b2));	//对象比较
    }
}
//结果
//true

Object class and reference data type

Example: Receive array data

public class Test1_1_4_7 {
    public static void main(String args[]){
        Object obj=new int[]{1,2,3};	//向上转型
        System.out.println(obj);	//数组编码:[I@10f87f48
        if (obj instanceof int[]){	//谁是否int数组
            int data[]=(int[])obj;	//向下转型
            for (int x=0;x<data.length;x++){
                System.out.print(data[x]+"、");
            }
        }
    }
}
//结果
//[I@10f87f48
//1、2、3、

Regarding the encoding
of array objects : As long as it is the direct output of the array object, the first digit is "[", the second digit is the short mark of the array type, for example: the int array is I, the double array is D, and then The encoding of the array object.

Example: Object class accepts interface objects

interface A{
    public void fun();
}
class B extends Object implements A{	//所有类一定继承Object类,所以此处只是强调说明
    public void fun(){
        System.out.println("666");
    }
    public String toString(){
        return "2333";
    }
}
public class Test1_1_4_8 {
    public static void main(String args[]){
        A a=new B();	//实例化接口对象
        Object obj=a;	//接受接口对象
        A t=(A)obj;	//向下转型
        t.fun();	//直接调用出口方法
        System.out.println(t);	//直接调用toString输出
    }
}
//结果
//666
//2333
49 original articles published · Liked 25 · Visits 1533

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/104334497