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();
}
}