Inheritance of object-oriented programming mechanism in C # learning notes

Source: Internet
Author: User

Inheritance reflects the relationship between classes and classes.

Many things in the world are common, the common part of which we are abstract as a base class, used to derive other classes, so as to improve the reusability of code, so that the structure of the code is clear and easy to read, and easy code extension and maintenance.

C # inheritance can inherit from only one base class, which differs from C + + inheritance.

C # Inheritance has transitivity, that is, b inherits from A,c inherits from B, and all of the attributes of a are.

The inheritance of C # is implicitly public.

If the call to a base class constructor is not displayed in the derived class constructor, the compiler automatically inserts a call to the base class's default constructor before executing the code in the derived class constructor, which causes a compilation error if the base class does not have a default constructor.

For example: Animal commonness: Having a mouth, having eyes, having a nose, being able to move, eating something

So define the base class (contains the animal's generality)


The code is as follows:


public class Animal {

......

}

When you define specific animals, you can inherit from the base class animal, without redefining these basic features, just define your own unique features.

For example: Dog


The code is as follows:


public class Dog:animal

{

The compiler automatically inserts a call to the default constructor of the base class before executing the code in the derived class builder

public string bark;//Dog barking

}


The code is as follows:


public class Dog:animal {

Calling a base class constructor in a derived class builder

Public Dog ()

: Base () {

}

public string Bark;

}


Attention:

You can assign the value of a derived class to a base class, but you cannot assign the value of a base class to a derived class, because the base class cannot be converted to a derived class, and the base class contains only a subset of the attributes of the derived class.

Animal animal=new Dog (); The right

Dog dog=new Animal (); error

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Inheritance of object-oriented programming mechanism in C # learning notes

This address: http://www.paobuke.com/develop/c-develop/pbk23184.html






Related content C # assemblies and Reflections on C # Non-modal forms the difference between show () and Mode form ShowDialog () C # design Pattern Series Tutorial-proxy mode C # Implementing multithreaded Web Proxy server instances
C # Methods for executing external commands C # WinForm to start another project by running a program code instance as an administrator efficient C # coding optimization principle

Inheritance of object-oriented programming mechanism in C # learning notes

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.