About parent class reference pointing to subclass object

Source: Internet
Author: User

When many people first came into contact with the design model, the biggest headache was to encounter such a sentence: (parent class animal and subclass dog)

Animal A2 =   New Dog ();

What is this? What is the purpose? I just need to use the subclass object directly?

Let's explain:
-- What is this?
Re: This usage is called "parent class reference pointing to a subclass object" (similar to a tongue twister), or "parent class pointer pointing to a subclass object", which refers to defining a reference to a parent class, it actually points to the object created by the subclass. The function is equivalent to "upward transformation" of sub-classes, or "upward object ",
The preceding statement can also be written as follows:

Animal A2 =   New Animal ();
Dog dog =   New Dog ();
A2 = Dog;

The reference to assigning a subclass object to the parent class is an upward transformation. Animal a2 = new dog (); the new dog () in is to instantiate an object without a name, then it is converted to the reference A2 of the parent class. That's all.

-- What is the purpose?
Re: In general, an object instance is set to dead after it is created. For example, ifProgramIf the variable is dog, it must be changed in the future.Code. Therefore, no dead dog is written in advance, but the parent class animal is written. Then, where animal is used in the future, the dog instance and CAT instance can be replaced without changing the Instance name. To put it bluntly, it also reflects the characteristics of object-oriented "polymorphism.

The following is a simple example to better understand:

Class Animal
{
Private String type =   " Animal " ;
Public   Virtual   Void Showlegs ()
{
Console. writeline ( " This is an {0}, animal always has legs " , Type );
}
}
Class Dog: Animal
{
Private String type =   " Dog " ;
Public   Override   Void Showlegs ()
{
Console. writeline ( " This is a {0}, dog has four legs " , Type );
}
}
Class Glede: Animal
{
Private String type =   " Glede " ;
Public   Override   Void Showlegs ()
{
Console. writeline ( " This is a {0}, glede has two legs " , Type );
}
}

Class Test
{
Static   Void Main ( String [] ARGs)
{
Animal A1 =   New Animal ();
Animal A2 =   New Dog ();
Animal A3 =   New Glede ();
A1.showlegs ();
A2.showlegs ();
A3.showlegs ();
Console. Readline ();
}
}

 

Result:

This Is An animal, animal always has legs
This Is A dog, dog has four legs
This Is A glede, glede has two legs

 

If you really understand this, you should be in touch with the design pattern field.

Last nagging:

· Because A2 and A3 are subclass objects, they are also called subclass methods (the premise is that they all override the parent class methods, you can try to replace the override of the two sub-classes with new, and the results are completely different.) Some people call this the "Object-based principle ".

· There must be no mistakes in upward transformation, just like you say that "dogs are animals" is true. On the contrary, you must pay attention to downward transformation. You cannot say "animals are dogs ".

I do not know whether it means that there is no such thing...

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.