Javase Introductory Learning 24:java Object-oriented supplement

Source: Internet
Author: User

a the object class in Java

The object class is the parent class of all Java classes, and if a class is not explicitly identified with the extends keyword to inherit another class, 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 that describes the information about the current object,

when a string is connected to its type data (for example, System.ouy.println ("info" +person)), the ToString () method of the object class is automatically called. can also be

To override the ToString () method in a user-defined type as needed

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 ';}}

Operation Result:


2) Equals () method

The comparison is whether the reference to the object points to the same memory address. In general, when comparing two objects, compare their values to be consistent, so

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);//Compare object's address System.out.println (s1.equals (S2));//Compare object's contents        }}

Operation Result:


Two Object Transformation (casting)

A reference-type variable of a base class can point to the object of its subclass;

A reference to a base class does not have access to new members (properties and methods) of its child class objects;

You can use the reference variable instanceof class name to determine whether the object that the reference variable points to belongs to the class or subclass of the class.

an object of a subclass can be used as an object of the base class to use as an upward transformation (upcasting), which is called a 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 encountered a lot, here is no longer said, you can refer to: Javase Primer Learning 18:java Object-oriented polymorphism.

three dynamic bindings (pool bindings) and polymorphic

Dynamic binding is the actual type of the referenced object that is judged during execution (not the compile time), and its corresponding method is called according to its actual 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 ();}}

Operation Result:


An understanding of extensibility:

Rewrite the above example:

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 ();}}

Operation 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 ();}}

Operation 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

Related Article

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.