a the object class in Java
The object class is the parent class for all Java classes. Assuming that a class does not inherit another class using the Extendskeyword clear identity, the class defaults
following object class.
public class person{ //}//equivalent to public class person extends object{ //}
method in the object class, suitable for all subclasses.
1) toString () method
The public string toString () method is defined in the object class and its return value is a string type. Describes information about the current object. In the
when a string is connected to its type data (for example, System.ouy.println ("info" +person)), it calls itself to the ToString () method of the object class. can also be
To override the ToString () method in the user-defined type
Instance:
public class Test{public static void Main (string[] arge) {Dog d = new Dog (); System.out.println (d); System.out.println ("D:" +d); System.out.println ("D:" +d.tostring ());}} Class Dog{public String toString () {return ' I am a Dog ';}}
Execution Result:
2) Equals () method
The comparison is whether the object's reference points to the same memory address. In general, the comparison of two objects is more consistent than their values. So to do
Rewrite.
the object class is defined as: Public Boolean equals (Object obj) method provides a definition object type
Instance:
public class Test{public static void Main (string[] arge) { string S1 = new String ("Hello"); String s2 = new string ("Hello"); System.out.println (S1 = = s2);//The address of the comparison object System.out.println (s1.equals (S2));//Compare the contents of the object }}
Execution Result:
Two Object Transformation (casting)
A reference-type variable of a base class can point to an object of its child class.
A reference to a base class is not able to access new members (properties and methods) of its subclass objects;
You can use a reference variable to instanceof the class name to infer whether the object pointed to by that reference variable belongs to the class or subclass of that class.
The object of a subclass can be used as an object of the base class to use called up Transformation (upcasting), which is called downward transformation (downcasting).
The upward transformation is a reference to a base class or parent class that points to a subclass object.
This in the previous example we met very much, here is no longer said, can participate: Javase Introductory Learning 18:java Object-oriented polymorphism.
three dynamic bindings (pool bindings) and polymorphic
Dynamic binding refers to the actual type of the referenced object that is inferred during run time (not the compilation period), and its corresponding method is called according to the de facto type.
In the following example. The corresponding enjoy () method is called based on the different actual types referenced by the member variable pet of the Lady object.
That's what you're new to.
That type is called the Enjoy () method of that type
Instance code:
Class Animal{private string name;//constructor method Animal (string name) {this.name = name;} public void Enjoy () {System.out.println ("barking ...");}} Class Cat extends Animal{private string eyescolor;//construction method Cat (String name,string eyescolor) {//Call the constructor of the parent class super (name); This.eyescolor = Eyescolor;} Override the Enjoy () method of the parent class Animal public void enjoy () {System.out.println ("cat cry ...");}} Class Dog extends Animal{private string furcolor;//construction method Dog (String name,string furcolor) {//Call the constructor of the parent class super (name); This.furcolor = Furcolor;} Override the Enjoy () method of the parent class Animal public void enjoy () {System.out.println ("barking ...");}} Class Lady{private string name;//reference type variable member private Animal pet;//constructor Lady (string Name,animal pet) {this.name = name; This.pet = Pet;} public void Mypetenjoy () {Pet.enjoy ();}} public class Test{public static void Main (string[] arge) {Cat c = new Cat ("CatName", "Blue");D og d = new Dog ("Dogname", "Blac K "); Lady L1 = new Lady ("L1", c); Lady L2 = new lady ("L2", d); L1.mypetenjoy (); L2.mypetenjoy ();}}
Execution Result:
An understanding of extensibility:
Rewrite the examples above:
Add a bird class:
Class Bird extends animal{//constructor method Bird () {//Call the constructor of the parent class super ("Bird");} Override the Enjoy () method of the parent class Animal public void enjoy () {System.out.println ("bird cry ...");}}
Overwrite the Main method:
public class Test{public static void Main (string[] arge) {Cat c = new Cat ("CatName", "Blue");D og d = new Dog ("Dogname", "Blac K "); Bird B = new Bird (); Lady L1 = new Lady ("L1", c); Lady L2 = new lady ("L2", D); Lady L3 = new Lady ("L3", b); L1.mypetenjoy (); L2.mypetenjoy (); L3.mypetenjoy ();}}
Execution Result:
the importance of polymorphic properties for System Extensibility
Continue to rewrite birds:
Class Bird extends Animal{private string feathercolor;//constructor method Bird (String name,string feathercolor) {//Call the constructor method of the parent class super ( name); this.feathercolor = Feathercolor;} Override the Enjoy () method of the parent class Animal public void enjoy () {System.out.println ("bird cry ...");}}
To rewrite the Mian method:
public class Test{public static void Main (string[] arge) {Lady L4 = new Lady ("L4", New Bird ("Birdname", "green")); L4.mypeten Joy ();}}
Execution Result:
conditions for polymorphism:
1) to have inheritance
2) to have a rewrite
3) Parent class reference to child class object
Javase Introductory Learning 24:java Object-oriented supplement