Initial knowledge inheritance and polymorphism

Source: Internet
Author: User

The concept of integration

In C #, a class can inherit another class, and the inherited class is often referred to as the parent or base class. Classes that inherit other classes are called subclasses or derived classes. The definition of a derived class can add new data and functionality, and an instance of a derived class can directly use the data or functionality of the parent class, but an instance of the parent class cannot directly use the data or functionality defined by the child class

Inheritance is an important feature in object-oriented programming. An inheritance relationship represents an arrow in a class diagram with an arrow pointing to the parent class:

Base keyword and protected modifier

We know that the This keyword can represent an instance of the current class, through which you can access the members of the class itself, and in C # there is a base keyword that represents the parent class, which can be used to access the members of the parent class.

The Age property of the Empployee class can be accessed through base.age, as in the SE class.

A member in the parent class, if decorated with private, will be a private member and cannot be accessed by any other class. Any class can be accessed if it is set to public, and another modifier is provided in C # that is allowed to be accessed by its subclasses and not allowed by other non-subclasses.

The limit strength of three modifiers to class members is as follows:

Private>protected>public

Subclass constructors implicitly call parent class constructors

When you create an instance object of a subclass, the constructor of the parent class is called first, and if we do not display the specified call to the parent class, the constructor system automatically calls the parent class's parameterless construct implicitly, if the parent class does not have a parameterless construct, the specified call must be displayed for the parent class construct to invoke.

Displays the constructor that invokes the parent class

It says that using the base keyword can represent the parent class, so as long as you add it after the constructor of the subclass: base (the argument list) you can specify that the constructor calls the parent class's construct as follows:

 Public SE (int age,string Gender,string ID,string name,list<job> worklist,int _popularity):base(Age,gender,id,name,worklist)        {           this. _popularity = _popularity;       }
Inheritance uses inheritance for transitivity:

The transitive nature of inheritance means that a subclass has the characteristics of a parent class, and if the parent class is a subclass of another class, the class also has the characteristics of the parent class.

Trucks and buses, for example, feature cars, they are subcategories of cars, and trucks can be divided into small trucks and large trucks, and they all have trucks that are characteristic of trucks, and they all have the characteristics of cars, buses can be divided into single-storey and double-decker, and they all feature a bus, It is a subclass of buses, and they all have characteristics of cars.

Inheritance of the single root sex:

Inherited single-root refers to: A class cannot inherit multiple classes at the same time.

is keyword

The IS keyword is used to determine whether an object belongs to a given type. For example, if we create an SE class object (SE), we can use the IS keyword to determine whether he belongs to the Empployee class

if  is empployee) {    // belongs }else{    // does not belong }
The value of inheritance

Inheritance simulates the real-world relationship, which emphasizes everything in OOP, which is in line with our thinking direction of object-oriented programming.

Inheritance implements code reuse, and reasonable use of inheritance will be more concise code.

Inheritance makes the structure of the program clear, the hierarchy of subclasses and parents clear, and the final goal is that the subclass only focuses on the related behavior state of the child class without having to focus on the behavior and state of the parent class.

polymorphic virtual keyword

The method used by the virtual keyword is called the virtual method, and the virtual method has a method body. The syntax is as follows:

access modifier Virtual return value type  method name () {    // method Body }
Override keyword

A method that is decorated with the override keyword, called a method override. Virtual methods can be overridden. The method overrides the syntax as follows:

Access modifier  override   return value type  method name () {    // method Body }

Attention:

The virtual method in the parent class does not have to be overridden by subclasses, and the default implementation of the virtual method can be defined in the parent class, and the default implementation of the parent class is still executed if the subclass does not override the method. If the method is overridden, the method after which the subclass is overridden is executed.

What is polymorphic?

Polymorphism means that the same operation can have different interpretations when acting on different objects, resulting in different execution effects!

Implementation of multi-state implementation method rewriting

The Sayhi () method is defined in the parent class and the virtual method is defined with the virtual keyword.

The subclass's own Sayhi () method is always defined in subclasses, and overrides of the parent class Sayhi () method are implemented with the override keyword modifier.

Defines the parent class variable, initializes the parent class variable with the subclass object,
Employee ema =new  SE ("Max", "EMA",Gender.female, + +);

The Sayhi () method of the subclass can be dropped directly with this parent variable, and the system can invoke that method according to the type of the object runtime, thus realizing the polymorphism of the class.

Initial knowledge inheritance and polymorphism

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.