.NET Core CSharp primary articles 1-6 class inheritance and polymorphism

.NET Core CSharp primary articles 1-6

This section contains the class inheritance and polymorphism

Brief introduction

Finally we talked about two of the three characteristics of object-oriented features in - inheritance and polymorphism. By inheritance and polymorphism, we can be a very good class of expanding to an extreme. In the following sections explain, we are planing to inheritance and polymorphism analysis carried out from various aspects.

inherit

Inheritance is actually a very good understanding of grammar, implemented in C # inheritance using A: B, represents A class inherits class B. Subclass (called a derived class) inherited from the parent class (called a base class) inherits all like his father than children, but the children always have their own personality, but there are some fathers do not tell my children. If we understand this part of the case control characters from the previous visit, you can describe this "harmonious," the father-son relationship.

Consider the following example: Sebastian Aikui Si (parent) is a noble European court, 67 years old, he was a viscount, and mastered a terrible secret can not tell anyone, only to rot in in stomach. He has a son named Mike Yikui Si, since Sebastian Aikui Si frail, when the family needed money, he would let his son take the bank card to the atm machine to withdraw money. Son's car and his car exactly the same, so that each son when driving housekeeper must make it clear: "I want to drive my father's car.", Or: "Today I drive my own car." If he did not say, then by default the housekeeper son out of the car. His family have a large idle estates can be used to develop, as long as the consent of the father, then the sons are free to develop.

For this example, you need to come to the conclusion that:

  • If Sebastian Aikui Si (parent) is not born, then Mike Yikui Si (subclass) does not exist. (You must first call the parent class constructor when the subclass is instantiated)
  • For Sebastian Yikui Si (parent), his name belongs to everyone can know (public), anyone can easily get. But I want to have the name, but it can only be (to get the parent class field after inheritance) talents of his family can be.
  • Go to the ATM machine to withdraw money for it, withdrawal password can only be told his son, others can not get. (Protected can be accessed by subclasses, but the rest is not accessible)
  • Sebastian Yikui Si (parent) to know a secret, the secret of this talent only he knows (private), including his son could not know (subclass can not get private)
  • Because the father and son exactly the same car, the car needs to take special note (subclass and parent signature when the same function, a function of the parent class is hidden by default, unless explicit instructions)
  • Hacienda is the father, but the son can be taken transform (specifying the parent class functions as a virtual, subclass can override a parent class function)

Some dig through this analysis, I think you should have seven or eight clear succession mean. It should introduce our base keyword, base keyword is reflected in a subclass method calls the parent class.

In particular, you should be noted that, C # supports only single inheritance for classes and class inheritance, if you are trying to achieve multiple inheritance, you can use the interface as the media and bridges.

We are now finished with a piece of code demonstrates our inheritance, our tutorial is to guide you more to think about, rather than letting some of you as quick out of the same course, no independent thinking.

class Father
{
    public Father()
    {
        Console.WriteLine("Father Method");
    }
    private string MSGSecret;
    protected string BankSecret;
    public string Name;
    public string Car;
    protected void test()
    {

    }
}
class Son:Father
{
    public Son()
    {
        Console.WriteLine("Son Method");
    }
    //隐式的生成了这三个字段
    //protected string BankSecret;
    //public string Name;
    //public string Car;
    public string Car;
    public void getCar()
    {
        base.Car;//父类
        this.Car;//子类
        base.test()//调用父类方法,如果签名不冲突,可以省略base
    }
}
Son s = new Son();//思考一下这里会输出什么?

Polymorphism

Polymorphism is not a difficult thing, inheriting and we talked about rewriting the interface is actually a type of polymorphism. Before we simply cited an example to illustrate polymorphism: carp humans are animals, but people breathe with lungs, breathing with gills and carp. This example reflects well on polymorphism. Use the following code to explain it.

class Animal
{
    //通过抽象方法实现多态
    abstract void Breathing();
    //虚方法实现多态
    virtual void Eat()
    {
    }
}
class Human:Animal
{
    override void Breathing()
    {

    }
    override void Eat()
    {

    }
}
class Fish:Animal
{
    override void Breathing()
    {
        
    }
    new void Eat()
    {

    }
}
Animal ah = new Human();
Animal af = new Fish();
Fish f = new Fish();

Note the last few lines, ah, and each belongs to Human and Fish af object, but when a method call, unified approach to call a parent class, and override the load and achieve sub-class.

ah.Eat();//调用父类的Eat(),但是以Human类中的重写方法为实现
af.Eat();
f.Eat();

What is the difference in these three? In fact, you only need to know, polymorphism is through the parent class to manage the sub-categories, so that subclasses conform to the specifications of the parent class. When subclasses inherit from the parent class when the method is to call the parent class method, but that implement subclasses. In particular, if you use the new modifier in the method, then this method will have no relationship and parent, as the meaning of the word, this is a function of a new function, not the parent class. Now you can try to analyze what methods of each class is above three function calls.

Polymorphic biggest bit is that you can or interface to manage through a base class to inherit all of their functions for subclasses, and can have a variety of methods to achieve the same function.

If you use interfaces and abstract classes to implement polymorphism method implementation it is the same, but is a base type can not be instantiated.

Github

BiliBili Home

WarrenRyan's Blog

Blog Park

Guess you like

Origin www.cnblogs.com/WarrenRyan/p/11241566.html