C # A summary of some obfuscation-prone concepts (6) --------- resolve Lishi replacement principles and Virtual Methods

Source: Internet
Author: User

First, let's replace the principle. This is essential to understanding polymorphism. The Wikipedia provides the following definition of the Lishi replacement principle: Why does the Child class Replace the parent class without affecting the function of the program? When inheritance is satisfied, the parent class must have non-private members, and the subclass must have these non-Private Members of the parent class (assuming that all the members of the parent class are private, therefore, sub-classes cannot inherit any member from the parent class, so there is no concept of inheritance ). Since the subclass inherits the non-Private Members of the parent class, the parent class object can call these non-private members in the subclass object. Therefore, the position of the parent class object can be replaced by the subclass object. Let's take a look at the following code: copy the code class Program {static void Main (string [] args) {Person p = new Person (); Person p1 = new Student (); Console. readKey () ;}} class Person {// private int nAge of the parent class; public Person () {Console. writeLine ("I'm a Person constructor, I'm a Person! ");} Public void Say () {Console. WriteLine (" I am a person! ") ;}} Class Student: Person {public Student () {Console. WriteLine (" I'm a Student constructor, and I'm a Student! ");} Public void SayStude () {Console. WriteLine (" I'm a student! ") ;}} Class SeniorStudent: Student {public SeniorStudent () {Console. WriteLine (" I'm a SeniorStudent constructor, and I'm a high school Student! ");} Public void SaySenior () {Console. WriteLine (" I'm a high school student! ") ;}} The result of copying the code is: according to the previous constructor knowledge, it is easy to explain this result. Then we add the following code to the Main () function: copy the code static void Main (string [] args) {Person p = new Person (); p. say (); Person p1 = new Student (); p1.Say (); Console. readKey ();} during the access to the code, we can find that p can only access the say of the parent class, while p1 can only access the Say method of the parent class. In fact, in the code above, this satisfies the Lee's replacement principle. The Student object of the subclass, replacing the location of the Person object of the parent class. So what happened to them in the memory? For example, you can know that when a parent class variable points to a subclass object, the parent class member can only be called through this parent class variable. The unique member of the subclass cannot be called. Likewise, we can infer that subclass variables cannot point to a parent class, but when a parent class variable points to a subclass variable, can I convert the variables of the parent class into subclass objects? Let's take a look at the two conversion methods of the reference type: we have learned from the above Code that a conversion type is directly added to the variable money. The following code: student s2 = (Student) p1; then the second conversion method is to use the as keyword, the following code: // convert the variable pointing to the subclass object to the subclass type Student s2 = (Student) p1; // If the as keyword is used, a null value Student s3 = p1 as Student is returned if the conversion fails. The difference between the as keyword and the first forced conversion is, if the conversion fails, an exception is thrown. If the conversion fails, a null value is returned. The Mind Map is summarized as follows: 2. virtual Methods use virtual keywords to modify them, which are called virtual methods (generally in the parent class ). Take a look at the following code: copy the code class Person {private int nAge; public Person () {Console. WriteLine ("I am a Person constructor, I am a Person! ") ;}// A virtual method public virtual void Say () {Console. WriteLine (" I am a person! ") ;}} Class Student: Person {// The subclass uses the override keyword to rewrite the virtual method public override void Say () {Console. WriteLine (" I am a Student! ");} Public Student () {Console. WriteLine (" I'm a Student constructor, and I'm a Student! ");} Public void SayStude () {Console. WriteLine (" I'm a student! ") ;}} Copy the code and add the following code to the main () function: copy the code static void Main (string [] args) {Person p = new Person (); p. say (); Person p1 = new Student (); p1.Say (); Student s = new Student (); s. say (); Console. readKey () ;}copy the code and print the result as follows: We can obviously find that the second expression meets the Rys replacement principle. p1.Say () should execute the statement () of the parent class () method, but the Say () method of the subclass is executed here. This means that the Say () method using the override keyword of the subclass overwrites the Say () method modified by the Virtual keyword of the parent class. Let's take a look at the debugging process using dynamic images. ① first, we didn't use any keywords: we can see that we directly jumped into the parent class and executed the Say () method of the parent class; ② let's look at the dynamic debugging images using the virtual and override keywords, as shown below: You can see the Say () method that executes the override keyword modifier directly to the subclass. What if the parent class is modified with the virtual keyword, but the subclass does not override this method? The following code: copy the code class Program {static void Main (string [] args) {Person p1 = new Student (); p1.Say (); Console. readKey () ;}} class Person {private int nAge; public Person () {Console. writeLine ("I'm a Person constructor, I'm a Person! ") ;}// A virtual method public virtual void Say () {Console. WriteLine (" I am a person! ") ;}} Class Student: Person {// The subclass does not contain the override keyword modifier public void SayStude () {Console. WriteLine (" I am a Student! ");}}

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.