C# Basics II

One: abstract class

Abstract classes cannot be instantiated:

Abstract classes are generally used as base classes that do not require writing methods

Abstract class methods do not need to write concrete methods.

 Override is still required when inheriting the abstract method of the base class

 

 Notice:

Although the abstract class parent class cannot be instantiated, the parent class object name can refer to the subclass entity object

Two: interface

 Interfaces can generally be similar to methods that need to be inherited by many subclasses, but this method does not require subclasses to implement

But if the interface writes a static method, then this method must be concretely implemented

And the interface cannot be instantiated

And all methods of the interface are public by default

As follows, the interface IFly; generally speaking, the interface starts with I by default, and rewriting does not require override

interface IFly{
    void Fly();
}

public class Bird:IFly{
    private string _name;
    public string Name{
        get{return _name;}
        set{_name=value;}
    }
    public void Fly()
    {
        Debug.Log($"鸟:{this.Name},在天空飞行.......");
    }
}

 If we need multiple inheritance then Bird:Pet,IFly

Three: Polymorphism

If we have two classes that inherit from IFly. Supposed to be Bird and Plane

We use

IFly ifly1=new Bird("(specific parameters)");

IFly ifly2=new Plane("(specific parameters)");

ifly1.fly();

ifly2.fly();

The methods used by these two should be methods inherited from their respective classes, that is to say, methods in subclasses

When executing a method, it will be executed differently according to the method in the class of the entity object itself, that is, the same code, multiple execution effects, which is the so-called "polymorphism"

 

 

Guess you like

Origin blog.csdn.net/jijikok/article/details/128807465