Rys replacement principle

Source: Internet
Author: User

If you have any questions, Gu Ge is required. I'm dizzy with what Gu Ge says. How can I useProblemHow can we understand the Lee's replacement principle?


     Class Fax
{
Public Void Faxdosome ()
{
Console. writeline ( " Fax's public Method " );
}
Protected Void Selfdo ()
{
Console. writeline ( " Fax's protected Method " );
}
}
Class Laserprinter: Fax
{
Public Void Laserprinterdosome ()
{
Console. writeline ( " Laserprinter's public Method " );
}
Private Static Void Do ()
{
Console. writeline ( " Laserprinter's private Method " );
}
}
Class Program
{
Private Static T min <t> (t o, t B) Where T: Class , Icomparable <t>
{
If (O. compareto (B) < 0 ) Return O;
Return B;
}
Static Void Main ()
{
Fax F = New Laserprinter ();
F. faxdosome (); // Only one fax method can be called.
Console. writeline (F. GetType ()); // LaserprinterSince it is laserprinter, why can't we call the laserprinter method??
Console. writeline (F Is Fax ); // True
Console. writeline (F Is Laserprinter ); // True FWhat type is it .. Why is it true?
Console. writeline (F Is Object ); // At this point, I understand why it is true. HoweverF. GetType (). Why is it laserprinter??
// Lee's replacement principle! What should I do? Which of the following experts can help you make common progress!
Console. readkey ();
}

}
 
Based on this example, how can we understand the Lee's replacement principle !? Which of the following experts can help you make common progress!
InCodeA large part of the logical functions are implemented using the parent class objects. Specifically, you only need to convert this subclass object into a parent class object at the beginning of the Code. The lining replacement principle ensures that the two sub-classes have the same attributes and methods, and can both be replaced with parent class objects, which are executed in the Code as parent class objects, in this way, most logic code about the parent class can be reused to the maximum extent. Subclass can override the method of the parent class. However, each subclass must implement every method used by the parent class in the logic code.
 
What kind of inspiration does the Li's replacement principle bring to us?
Class inheritance principle: if an object of an inherited class may encounter a running error in the base class, the subclass should not inherit from the base class, or, the relationship between them should be re-designed.
Action correctness guarantee: class extensions that comply with the Li's replacement principle will not introduce new errors to existing systems.

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.