Inheritance (the process of [mutual conversion] Between subclass objects and parent objects)

Source: Internet
Author: User

<1>

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace list {Class A {} Class B: A {public a mymthod () {// you can see that the return value type of this mymthod () method is clearly a type, why is no error returned if a B-type object is returned? Return new B (); // because B is a subclass of type A, the subclass object can be implicitly converted to the parent class object. Here we have implicitly converted type B to type A and returned it. Therefore, no error is reported.} Class program {static void main (string [] ARGs) {A = new A (); B = new B (); A = B; // a subclass object can be assigned a value to a parent class Object // B = A; // a parent class object cannot be assigned to a subclass object B = (B) A; // For example: if the parent class is a "person", it has two sub-classes: A "Chinese" class, A "American" class // That is to say, a new Chinese object "bruce lee" New an American object "Obama" // a new "person" object "person 1" // subclass target: Bruce Lee ", "Obama" can certainly be assigned to "person", the parent object 1. Because "person 1" can be Chinese or American, as long as it is a person. Here you can give me a "bruce lee" or "Obama. // The parent class Object "MAN 1" is assigned to the subclass object "bruce lee" or the subclass object "Obama. // Because "bruce lee" is a Chinese type, the Chinese type can only receive the Chinese type, so when you assign a "person" type object to the "Chinese" type object, an error occurs. If you want a Chinese person, you can give me one person. How do I know if your "person 1" is Chinese? You may also be American or German. Right. // Unless you specify to me that this person must be a Chinese person. "Bruce Lee" = (Chinese) "MAN 1 "}}}


Inheritance (the process of [mutual conversion] Between subclass objects and parent objects)

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.