C #'s replacement principle,

Source: Internet
Author: User

C #'s replacement principle,

Lee's conversion Principle
Sub-classes can be assigned to parent class objects.
The parent class object can be forcibly converted to the corresponding subclass object.


The principle of RYS replacement is intuitively understood as "sub-class is parent class", which in turn won't work.
Just like a man is right, but a man is wrong.
This is clear.


In the object-oriented thinking, it can be seen that a derived class has all the features that the base class exposes downward. It is a special case of the base class.
When a derived class object is assigned to a base class type, the data structure of the derived class corresponds to the data structure of the Base class in sequence. The data of the derived class is invisible.

When the base class object attempts to convert to a derived type, the data of the base class object cannot be filled with all the data structures of the derived class in sequence. This makes it impossible to complete the function defined by the derived class. The compiler will prompt or even report an error.
This is why the derived class is competent for the basic class function, but the base class is not fully competent for the function of the derived class.
Forced conversion belongs to the process from the base class to the derivation: the designer knows that the data structure of the Base Class Object can completely fill the structure of the derived class. Otherwise, a forced conversion error occurs. It is generally best to avoid strong conversion!

Also, it is not entirely correct that subclass can appear in any parent class object, and sometimes some members of the parent class will not be exposed to the subclass.
C # sample code: http://www.eyesourcecode.com/f/CSharp/1

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.