Although the csdn account has been registered for a long time, I rarely came in and blogged for the first time, because I am still a student in my studies. I have a little experience after watching the learning video today. I have the right to keep a diary. If a big man passes by, I hope to give him some advice.
1. What is an Object class
In my understanding, the Object class is similar to the existence of the caveman - the ancestor of human beings, who regarded us at different times and even different people as a class, and the caveman is the common parent of all of us. We all have his own characteristics and have evolved and influenced by various factors to become different people. There are also features (attributes and methods) that distinguish them from their ancestors.
2. Several commonly used methods (incomplete, relatively important for the time being)
Stundent class: (general operation, skip)
class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
(1) xxx.getclass( )
The getclass() method is a local method to get the corresponding type of the object (native, you can view the source code), and the output from the console is the fully qualified name (package path + class name), which is also one of the reflex methods to get the object that you will learn later. The following example is:
public static void main(String[] args) { Student stu1 =new Student("Leaflet",22); System.out.println(stu1.getClass()); }
The console output is as follows:
(2)xxx.hashcode( )
The hashcode() method, which is to get the hash code of the changed object, is based on the value of the physical address and is obtained by a series of operations. This is a local method. The details of the operation are not clear, and the lobule is still waiting for the big man to point out. When we new out an object, a hash value will be generated. Hash values are different and will not be the same object, but hash values are the same and not necessarily the same object.
How to interpret the second half of the sentence, because when we customize a class that has not overridden this method, its object remains the method of the Object itself when it is called. As a subclass of the defined class, the hashcode method can be overridden in the class. The ecplise shortcut (shift+alt+s) can be found. Assigning the same value to different objects in the same class will result in the same hash value, because instead of relying on the value of the physical address as in the Object method, you will be given a way to calculate the hash value based on its method (You can also write it yourself, just override here). The name attribute in Student is a String class, which has already overridden the hashcode() method. As you can see in the following example, the code and output of the comparison before and after override are as follows:
Prerewrite code:
public static void main(String[] args) { Student stu1 =new Student("Leaflet",22); Student stu2 =new Student("Leaflet",22); System.out.println(stu1.hashCode()); System.out.println(stu2.hashCode()); }
The output is:
The overridden hashcode() method code in the Student class is:
public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; }
The output is:
(3)xxx.equals( )
The equals() method is used to determine whether two objects are the same object or not. It provides java with a method that is not local. Similar to the hashcode() method, there is a need to override the Object class method in a class to achieve the specific function of the equals() method.
Before it is overridden, and==are used to determine if the addresses of the two objects are the same. The equals () method should be used to determine if the contents of two objects are the same. Like the hashcode () method, custom classes need to be rewritten like other classes (such as String class), rewriting the code and output of the comparison before and after:
Prerewrite code:
public static void main(String[] args) { Student stu1 =new Student("Leaflet",22); Student stu2 =new Student("Leaflet",22); System.out.println(stu1.equals(stu2)); System.out.println(stu1==stu2); }
Output:
Rewritten equals() method code:
public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }
If you have a partner who can't read the rewritten method, you can leave a message. There aren't too many notes here
public static void main(String[] args) { Student stu1 =new Student("Leaflet",22); Student stu2 =new Student("Leaflet",22); System.out.println(stu1.equals(stu2)); System.out.println(stu1==stu2); }
Output:
(4)xxx.clone( )
Clone means clone. The clone() method makes a copy of an object, not an assignment. It is wrong for an object to assign a direct value to complete the copy function. If you change the attributes in the assigned object, the attributes of the assigned object will change as well. The code and output are as follows:
public static void main(String[] args) { Student stu3 =new Student("Leaflet",22); //Assignment is not a copy Student stu4 =stu3.clone(); stu4.setAge(10); System.out.println(stu3); System.out.println(stu4); }
Output:
Reason: Assignment is an assignment in the stack. There is only one object data in the stack. When the object data is changed, the objects with the same address in the stack will change because they share this data.
Duplication is divided into deep duplication and shallow duplication. Let's talk about the difference between the two types of duplication and what is duplicated.
A. Shallow copy (basic data types can only be copied, reference types are still common)
Because the clone method itself is a protected access modifier-modified method that can only be accessed by itself, by the same package, and by its subclasses, it can no longer be called directly from the main method in the Test class. Instead, it should be called from its subclass Student class. If you want to call this method outside of the class, you can only override it and you should expand the access range. That is, to rewrite protected to public, as shown in the following code:
public Student clone() { Student s =null; try { s= (Student)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return s; }
In overrides, the return value type can be a subclass of the parent class, that is, a Student class. The reason for calling super is that the clone() method in the Object class can achieve its functions without rewriting them, while the s object is a Student class, so it needs to be reinvented once so that the calling object can be copied, and then assigned to a new object through the code above to complete the replication.
Not finished yet, not only override the method, but also implement the tag interface Cloneable, which declares that the class can be cloned, as shown in the diagram
This completes the cloning with the following output:
B. Deep copy (copy the reference type again)
If you import data of a reference type, you will not be able to copy its data, because it is a new object data for objects of a basic data type, and they will share the object data (again a shallow copy), which will change for all objects when the object data changes. The following figure shows:
Introduce an Address class:
class Address{ String add; int number; public Address(String add, int number) { super(); this.add = add; this.number = number; } @Override public String toString() { return "Adress [add=" + add + ", number=" + number + "]"; } }
The code is:
public static void main(String[] args) { Student stu3 =new Student("Leaflet",22,new Address("global village",001)); //Assignment is not a copy Student stu4 =stu3.clone(); stu4.setAge(10); stu4.address.add=("Lunar Village"); System.out.println(stu3); System.out.println(stu4); }
The output is:
How to achieve deep replication, the same principle as shallow replication, in one pass, implement the clone interface on the Address class (no longer shown), and override the clone method again, as shown in the following code:
public Address clone() { Address a =null; try { a=(Address)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return a; }
The output is: