My Java Learning notes (9) About object: Superclass for all classes

Source: Internet
Author: User
Tags class manager

The 1.Object class is the ancestor of all Java classes, and every class in Java is extended by it.

2. If the superclass is not explicitly indicated, object is considered a superclass of this class.


Class A extends Objectclass A//can default

3. You can reference any type of object using the object type variable.


Object e = new Employee ("W1", 100,1988,1,1);


4. In Java, only the base type is not an object, and an array type, whether an object array or a basic and type array, can be extended to the object class.


Int[] A = new Int[3];object obj = A;obj = new INT[10];


the Equals method in the 5.Object class is used to detect whether an object is equal to another object. This method determines whether two objects have the same reference.

6. When defining the Equals method in a subclass, the equals of the superclass is called first. If the detection fails, the objects cannot be equal, and if the fields in the superclass are equal, then the entity domains in the subclasses need to be compared.
The 7.java language specification requires the Equals method to have: A. reflexivity B. Symmetry c. transitivity d. Consistency E. Non-null reference returns FALSE.

8. The idea of writing a equals method:

A. Detects whether the same object is referenced.

B. Detects if the comparison object is null.

C. Detect if it belongs to the same class.

D. Compare the entity fields in the class.

9. The hash code is an integer value exported by the object. There is no regularity.

10. Because the Hashcode method is defined in the object class, each object has a default hash code whose value is the object's storage address.

The 11.hashCode method should return an integer value and logically organize the hash code of the instance domain so that each different object can produce the same hash code.

The definition of the 12.equals method and the Hashcode method must be consistent: if X.equals (y) returns True, then X.hashcode () must have the same value as Y.hashcode ().

13. In the object class, the ToString method is used to return a string representing the object worth.

14. If X is any object and calls the

SYSTEM.OUT.PRINTLN (x);
the Println method calls X.tostring () directly and prints the string.

The 15.Object class defines the ToString method, which is used to print the class name and hash code that the output object belongs to.

Related instance Programs

Test class


public class Test {public static void main (string[] args) {Employee W1 = new Employee ("Worker1", 1000,1989,1,1); Employee W2 = W1; Employee W3 = new Employee ("Worker1", 1000,1989,1,1); Employee W4 = new Employee ("Worker4", 2000,1991,1,1); System.out.println ("W1 = = W2:" + (W1==W2)); System.out.println ("W1 = = W3:" + (W1==W3)); System.out.println ("W1.equals (W3):" + w1.equals (W3)); System.out.println ("W1.equals (W4):" + w1.equals (W4)); System.out.println ("w4.tostring ():" + W4); Manager m1 = new manager ("M1", 1000,1981,1,1); Manager m2 = new manager ("M1", 1000,1981,1,1); M2.setbonus (100); System.out.println ("m2.tostring ():" + m2); System.out.println ("M1.equals (m2):" + m1.equals (m2)); System.out.println ("W1.hashcode ():" + W1.hashcode ()); System.out.println ("W3.hashcode ():" + W3.hashcode ()); System.out.println ("W4.hahacode ():" + W4.hashcode ()); System.out.println ("M1.hahacode ():" + M1.hashcode ()); System.out.println ("M2.hahacode ():" + M2.hashcode ());}}


Employee Class


Import java.util.*;p Ublic class Employee {private String name;private Date hireday;private double salary;public Employee ( String n,double s,int year,int month,int day) {name = N;salary = S; GregorianCalendar gr = new GregorianCalendar (year,month-1,day); hireday = Gr.gettime ();} Public String GetName () {return name;} Public double getsalary () {return salary;} Public Date Gethireday () {return hireday;} public void Raisesalary (double p) {Double raise = salary * p/100;salary + = Raise;} public boolean equals (Object OtO) {if (this = OtO) return true;if (OtO = = null) return False;if (GetClass ()! = OtO) return Fals E Employee o = (employee) Oto;return objects.equals (name, o.name) && salary = = o.salary && objects.equals (hi Reday, o.hireday);} public int hashcode () {return objects.hash (name,salary,hireday);} Public String toString () {return getclass (). GetName () + "name =" + name + ", salary =" + Salary + ", Hireday =" + Hireday;}}

Manager Class


Public class Manager extends Employee {private double bonus;public Manager (String n,double s,int year,int Month,int day) {s Uper (n,s,year,month,day); bonus = 0;} Public double getsalary () {Double basesalary = super.getsalary (); return basesalary + bonus;} public void Setbonus (double b) {bonus = b;} public boolean equals (Object OtO) {if (!super.equals (OtO)) return false; Manager m = (manager) Oto;return bonus = = M.bonus;} public int hashcode () {return Super.hashcode () + one * New Double (bonus). Hashcode (); Public String toString () {return super.tostring () + "bonus =" + Bonus;}}

Output Results





My Java Learning notes (9) About object: Superclass for all classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.