Re-learning "C # Advanced Programming" (Inheritance)

Source: Internet
Author: User

The first two days saw the fourth chapter in advanced Programming in C #: Inheritance and Chapter Sixth: arrays. Three major features of OOP: encapsulation, inheritance, polymorphism, personal feeling inheritance is the basis for the realization of polymorphism, including the design patterns of future contact, are derived from inheritance characteristics.

There are two inheritance attributes, which implement inheritance and interface inheritance .

  Implementing inheritance

1      Public classA2     {3         Private string_str;4          Public stringSTR {Get{return_STR; }Set{_str =value;} }5 6          Public voidAmethod ()7         {8         }9     }Ten  One      Public classb:a A     { -         Private int_num; -          Public intNum {Get{return_num; }Set{_num =value;} } the  -          Public voidBmethod () -         {  -         } +}

In the above example, Class B inherits from Class A, it can have an open attribute and method of a, these are the basis, do not explain each one, their reading faster, too lazy to say. Under the ordinary use of less, easy to forget things, one is a constructor, the constructor of the inheritance system is from the parent function level downward constructs.

For example, in the example above: Instantiating a Class B, it instantiates a parent class, a Class A, before instantiating Class B.

Refactoring method: When class B needs to create a new method that already exists in Class A, if the Class A method has a tag that can be refactored, then Class B can add the keyword override to refactor the method in the new method, otherwise you will need to add a new before the class B method Keyword to hide the Class A method.

  Interface inheritance

The so-called interface interface inheritance, according to the non-standard is actually the implementation of interfaces. Interface Specification There is a relatively easy to ignore, the interface can regulate the properties (not the field), the others seem to be similar. Personal feeling, interface is like a regulation, you want to join a circle, you must abide by the rules and regulations of a certain circle.

For example: To take part in a race, be sure to sign up for a race, have a car, and compete. Among them "register for the competition", "there is a car", "participate in the game", on the abstraction of the interface inside the method and properties. To participate in the race car, you have to implement this interface, that is, these methods and properties. The car to achieve this interface, but will not limit how you achieve, registration can register, online registration, telephone registration ... The car can be BMW, Mercedes-Benz, Land Rover, Honda ..... Race, you can run 50 yards, you can run 40 yards, you can even stop on the track

Re-learn C # Advanced Programming (Inheritance)

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.