[You must know. NET] The 15th time: The theory of inheriting essence

Source: Internet
Author: User
Tags abstract inheritance

This article describes the following:

What is inheritance?

The realization essence of inheritance

1. Introduction

About inheritance, whether you are familiar with the light, about inheritance, whether you know at all.

This article does not discuss the basic concept of inheritance, we return to the essence, from the compiler operating point of view to reveal. NET inheritance, to discover how the subclass object implements the inheritance of the parent member and method, reveals the essence of inheritance in the simplest example, and explains how the inheritance mechanism is executed, which is necessary and inevitable for a better understanding of inheritance.

2. Analysis

The following is an example of a simple animal inheritance system:

public abstract class Animal
{
public abstract void ShowType ();
public void Eat ()
{
Console.WriteLine ("Animal always Eat.");        
}
}
public class Bird:animal
{
private String type = ' Bird ';
        public override void ShowType ()
{
Console.WriteLine (' type is {0} ', type);

private string color;
public string color
{
Get {return Color;}
Set {color = value;}        
}
}
public class Chicken:bird
{
private String type = ' chicken ';
        public override void ShowType ()
{
Console.WriteLine (' type is {0} ', type);
       
public void Showcolor ()
{
Console.WriteLine (' Color is {0} ', color);
}
}

Then, create each class object in the test class, and because animal is an abstract class, we only create bird objects and chicken objects.

public class TestInheritance
    {
        public static void Main()
        {
            Bird bird = new Bird();
            Chicken chicken = new Chicken();
        }
}

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.