The previous section inherits the following (i) We do abstract classes, interfaces, and polymorphic learning,
Next thing we're going to tell you something special is the object class,
We have been saying that inheritance, the son inherited the father, the father has no parent class,
Why do you think that the first line of the constructor is not all there
An implicit super () statement, they are not going to visit their own
What about the parent class? Actually, yes, what's the name of this parent class?
Called the object class, the legend of the Java object of God, haha.
1. Definition
The superclass of all objects, the direct latter of all objects, the parent class.
Must be a feature that all objects have.
To define a class casually
Class demo{} In addition to the implicit construction method, itself has a lot of methods.
Attention:
For example, because of the Equals () function, Java considers all objects to be of a comparative nature,
Can be compared between two objects is the same, you can understand it? As long as you're an entity,
It can compare two objects that are not the same address.
equals can compare the two objects created, but is this more meaningful?
2. Example
In the demo I want to build my own comparison to compare the characteristics of my demo object,
How do we do that at this time? Do you want to define the function yourself?
For example, I have this action in my demo, which means that I have a number encapsulated in the demo,
Who do I want to compare? I don't want to compare the address values of the demo object,
What I want to compare is whether this number is the same in the demo, OK?
You can also set it to age, This.num=num,
So, are we going to build a comparative act? Who are the parameters? Demo D
Class demo{ private int num; Demo (int num) { this.num=num; } public boolean compare (Demo D) { return this.num==d.num; }}
But let's think about our demo. Does this class have the Equals () function,
Parent class object We provide a way to compare the parent class, why do you want to write again?
is not directly to use can. We directly replicate the Equals () method in object.
Class demo{ private int num; Demo (int num) { this.num=num; } public boolean equals (Object D) { return this.num==d.num; }}
But there will be
What is this for? Because there is no downward transition,
Num belongs to this class of member function and does not exist in object.
Class demo{ private int num; Demo (int num) { this.num=num; } public boolean equals (Object obj) { demo d= (demo) obj; Transition down to return this.num==d.num;} }
But what if you pass in different objects? So are you using instanceof?
Judging if obj is not equal to Demo
Class demo{ private int num; Demo (int num) { this.num=num; } public boolean equals (Object obj) { if (!) ( obj instanceof Demo)) return false; Demo d= (demo) obj; Transition down to return this.num==d.num;} }
Overall
Equals () In Object This method compares two objects by default for equality.
But is it not very meaningful to compare objects equal, so we build
How does the object's own content compare? Replication!
Because the parent class is already defined, but don't forget that if you use the data that is unique to the object
The object to be judged and transformed. The above is the Equals () method feature in object.
Different Java Learning (vi) inheritance under (2.1) object class Equals ()