当前位置:网站首页>Object class not ended

Object class not ended

2022-06-12 05:00:00 なんでもないゃ

Object Class is Java Base class of all classes , Is the top of the entire class inheritance structure , It's also the most abstract class .

Object contains :registerNatives()、getClass()、hasCode()、equals()、clone()、toString()、notify()、notifyAll()、wait(long)、wait(long,int)、wait()、finalize() Twelve methods .

        registerNatives()

        getClass()

public final natove Class getClass();

public Methods can be called directly through objects ,getClass The method is to get the collection of all the information of the object of the current class at runtime .

        hashCode()

public native int hashCode();

public Method subclasses can override it ,hashCode Method returns the address of the current object by default hashCode value , After rewriting, it is the custom calculation method , The value is in the range of integers (-2^31~2^31-1).

import java.util.stream.Stream;

public class lengthannotation {
        private int age;
        private String name;
// Pay attention to the creation and use of constructors ;
    lengthannotation(int age,String name){
        this.age=age;
        this.name=name;
    }
        public int hashCode () {
            //Stream.of() Create a sequential flow for the specified element ;
            //Stream.toArray() Method returns Object[] Array ;
            Object[] a = Stream.of(age, name).toArray();
            int result = 1;
            for (Object element : a) {
                result = 31 * result + (element == null ? 0 : element.hashCode());
            }
            return result;
        }
        public static void main(String[] args) {
            // Call... After creating the object hashCode();
            lengthannotation length=new lengthannotation(1," Zhao Zhao ");
            System.out.println(length.hashCode());
    }
}

        equals()

public boolean equals(Object obj);

Used to compare whether the current object and the target object are equal , The default is to compare references ( Object address ) Whether to point to the same object , Subclasses can override , After being overridden within a class, the overridden equals Method .

The reason for rewriting : When defining an array of objects that do not allow duplicate values ,equals Method determines the address of the object ( quote ), No matter what the value is ,new The memory address of the object must be different , therefore equals Method cannot be judged , Need to rewrite .

Be careful : In rewriting equals Methods are also generally overridden hashCode Method .

No rewriting equals() when :

public class Equalstest {
    public static void main(String[] args) {
        // establish List list students The storage type is Students The elements of 
        List<Student> students = new ArrayList<>();
        // Statement Student Type of student object , Note that it is a statement , Not created ;
        Student student;
        for(int i=0;i<10;i++){
            // establish student object , Each iteration creates an object ;
            student=new Student(" Xiao Ming ");
            // Judge students Whether this... Is included in the list student, If not , Add to list 
            //contains() Method , Determine whether the gift box contains an element , With the return true, No return false;
            // Be careful :contains() Source code used in equals() Method 
            if(!students.contains(student)){
                students.add(student);
            }
        }
        System.out.println(students.size());// Output 10
    }
}
class Student{
    private String name;
    public Student(String name){
       this.name=name;
    }
}

Rewrote equals() when :

public class Equalstest2 {
    public static void main(String[] args) {
        List<Student2> students=new ArrayList<>();
        Student2 student;
        for(int i=0;i<10;i++){
            student=new Student2(" Xiao Ming ");
            if(!students.contains(student)){
                students.add(student);
            }
        }
        System.out.println(students.size());//1
    }
}
class Student2{
    private String name;
    public Student2(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    // rewrite equals Method 
    public boolean equals(Object obj){
        if(obj==this){
            return true;
        }
        if(obj instanceof Student2){
            Student2 student = (Student2) obj;
            if(student.getName().equals(this.name)){
                return true;
            }
        }
        return false;
    }
}

        clone()

protected native Object clone() throws CloneNotSupportedException;

protected Method , Provided to subclass override , But it needs to be realized Cloneable Method , This is a tag interface , If not , When calling object.clone() Method , Will throw out CloneNotSupportedException.

Be careful : Use clone() Method must be overridden clone() Method .

        toString()

public String toString();

public Method , Subclasses can override , default toString Method returns the fully qualified class name of the current class [email protected]+ Hexadecimal hashCode value .

How to rewrite : Write what you want to return .

	public String toString() {
		return "[name = " + getName() + ",age = "+ getAge() + "]";
	}

        notify()

        notifyAll()

        wait(long)

        wait(long,int)

        wait()

These three methods are used for inter thread communication , The function is to block the current thread , Wait for another thread to call notify()/notifyAll() Method to wake it up . All three methods are public final Of , Cannot be rewritten .

        finalize()

原网站

版权声明
本文为[なんでもないゃ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010622133445.html