java.lang.Object (array collection object & redefine)

In Java, a subclass can only inherit one parent class. If you define a class without using the extends keyword to specify any class to inherit, it must inherit java.lang.object. That is, if you define a class like this:

public class Some{
...
}

That would be equivalent to writing:

public class Some extends Object{
}

Therefore, in Java, any class traced back to the top-level parent class must be java.lang.Object, that is, all objects in Java, must be "a kind of" object, so it is legal to write a program like this:

Object o1="李白"Object o2=new Date();

String is an object, Date is an object, any type of object can be referenced using the name declared by the oblect. What's the benefit of this? If there is a requirement to use an array to collect various objects, then declare object[]. E.g:

Object[] objs= {"李白",new Date(),new SwordsMan()};
    String name=(String) objs[0];
    Date date=(Date) objs[1];
    ...

Because the length of the array is limited, it is not so convenient to use the array to collect objects. The Arraylist class defined below can collect objects of unlimited length:

package hello;

import java.util.Arrays;

public class ArrayList {
    private Object[] list;
    private int next;

    public ArrayList(int capacity) {
        list=new Object[capacity];
    }
    public ArrayList() {
        this(3);
    }
    public void add(Object o) {
        if(next==list.length) {
            list=Arrays.copyOf(list, list.length*2);
        }
        list[next++]=o;
    }
    public Object get(int index) {
        return list[index];
    }
    public int size() {
        return next;
    }
}

The custom Arraylist class uses the Object array internally to collect object 0. Each collected object will be placed at the index specified by next. When creating an Arraylist instance, you can specify the initial capacity of the internal array. If you use the parameterless constructor, then The default capacity is 3. If you want to collect objects, you can use the add() method. Note that the type of the parameter is Object, which can receive any object. If the original length of the internal array is not enough, use the Arrays.copyof() method to automatically create an array with twice the original length and copy the elements ⊙ . If you want to get the collected object, you can use get() to specify the index to get it. If you want to know the number of objects collected, you can use the size() method.
The following uses a custom Arraylist class to collect visitor names and convert the list to uppercase and display it:

package hello;

import java.util.*;
import static java.lang.System.*;

public class Guest {
    public static void main(String[] args) {
        ArrayList datas=new ArrayList();
        collectDataTo(datas);
        out.println("访客名单:");
        printUpperCase(datas);
    }

    private static void collectDataTo(ArrayList datas) {
        Scanner scan=new Scanner(System.in);
        while(true) {
            out.println("访客名单");
            String data=scan.nextLine();
            if(data.equals("*")) {
                break;
            }
            datas.add(data);
        }
    }

    private static void printUpperCase(ArrayList datas) {
        for(int i=0;i<datas.size();i++) {
            String data =(String) datas.get(i);
            out.println(data.toUpperCase());
        }
    }
}

java.lang.Object is the top-level parent class of all classes, and methods on Object can be redefined as long as they are not defined as final.
1.
The toString() of the toString() object is defined by default as:

public String toString(){
    return getClass().getName()+"@"+Integer.toHexString(hashCode());
}

The returned string includes the class name and hexadecimal hash code, and many methods call toString() by default if an object is passed in. For example, the System.out.print() method will call toString() to get the string description to display:

System.out.println(swordsMan.toString);
//就是
System.out.println(swordsMan);

2. Redefine equals()
In Java, to compare the substantial equality of two objects, it is not using =, but through the quals method. Later, when you have seen Integer and other packaging, and string equality comparison, both are Use the equals() method. In fact, the equals() method is a method defined by the Object class, and its program code is:

public boolean equals(object obj){
    return (this==obj);
}

If you do not redefine equals(), when you use the equals0 method, the effect is equivalent to =, so to compare substantial equality, you must redefine it yourself. Such as:

package hello;

public class Cat {
    //......

    public boolean equals(Object other){
        if(this==other) {//如果other参考的就是这个对象肯定是同一对象。
            return true;
        }
        //如果other参考的对象不是Cat创建出来的就是flase
        if(!(other instanceof Cat)) {
            return false;
        }else if(!getName().equals(cat.getName())){
            return false;//名字是否相同
        }else if(!getBirthDay().equals(cat.getBirthDay())){
            return false;//生日是否相同
        }

        return true;
    }
}

This program fragment demonstrates the basic concepts of the equals() operation. The instanceof operator is also seen here. It can be used to determine whether the object is created by a certain class. The left operand is the object and the right operand is the class. When using instanceof, the compiler will also help, and will check the left operation. Whether the number type is in the inheritance structure of the right operand type (or in the interface operation structure)
during execution, not only the left operand object is directly instantiated by the right operand class to return true, as long as the left operand type is the right operand type The subtype of instanceof also returns true
. This only demonstrates the basic concept of the equals() operation. In fact, the operation of equals() is not so simple. Hashcode() is usually also manipulated when equals() is manipulated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324480788&siteId=291194637