.NET Core CSharp primary articles 1-5 interfaces, enumerations, abstract

.NET Core CSharp primary articles 1-5

This section class interfaces, enumerations, abstract

Brief introduction

problem

  • If you need a day of the week or certain states, using strings or numbers are not intuitive?
  • Do you find, no matter what kind of computer, its USB ports are designed to follow certain norms?

    enumerate

    Enumeration (enum) is a special type of value a very easy to use, he allows you to specify a series of character constants (typically starting from 0). It is defined and used as follows:
public enum Week
{
    Monday,
    ...//此处省略
    Sunday = 6//可赋值
}
bool flag = (6 == (int)Week.Sunday)

But you can also specify other enumeration type as a value, for example:

public enum Week:byte
{
    Monday,
    ...
    Sunday= 6
}

Other types of conversion and enumerated using casts can be, for example: (int) Week.Sunday, but in particular, does not need to cast 0 can be compared and enumeration.

Flag Enum

This is an interesting enumeration, it supports you to enumerate be operational by bit, using a flag enum need to specify Attribute name in the enumeration above, that is, [Flags], generally speaking, we will use as a power of 2 pieces For value, because essentially bitwise binary arithmetic.

Specific examples are as follows

[Flags]
public enum Status
{
    Success = 1,
    NotFound = 2,
    Fail = 4
}
//支持按位运算,运算步骤我们在之前已经有过详细的讲解
Status.Success | Status.NotFound

By default, if you enter an unreasonable enumeration value (ie not defined), the compiler will default to the digital output directly, but if you use a bitwise enumeration, then he will number you enter converted into binary values ​​with each enumeration & operator, the result is compared with the enumeration value, and if found will be output.

For example, the above embodiment Status:

Console.WriteLine((Status)7);
//输出是三个都输出
//7 = 0111,
//1 = 0001,
//2 = 0010
//4 = 0011

interface

Interface this thing, the novice is very easy to be misled, for instance in WebApi development, your front-end interface to a friend lets you give him, this time, what he needs in the mouth called the back-end API,
when your friends say the back-end, you write an interface, we use dependency injection for unified management class that implements the interface, this time, he needs is a convention, we say here is the interface (interface).

Interface is an important object-oriented C # syntax to achieve polymorphism. The definition of the interface can be understood as a convention specification. Such as USB-A port, the world's computer manufacturers are standardized production, if we do not produce agreement pressing, what would be the consequences?

In C #, the interface is the same that played a role, but there are some of the more widely used.

Defined using the interface keyword interface, the default, all interfaces are members access to the public, as is the need to publish specifications for all to see, if you define access rights would be no practical meaning
, and all interface functions there is no function body. In general it is that the interface is to provide a standard class of things, but do not provide an implementation.

Examples of interfaces

//定义一个接口
public inteface IHuman
{
    void Eat();
    bool Alive();
}
//接口的实现,必须实现每一个接口中的函数并保持返回类型、函数签名,函数参数一致
public class Human:IHuman
{
    void Eat()
    {

    }
    bool Alive()
    {
        return default<bool>();
    }
}

And we will achieve talked about behind inherited a very similar interface, where you only need to remember a class implements an interface to support multiple interfaces, but can only inherit a class can be.

Interface conflict

Because they support a class implements multiple interfaces, it is likely to result in A and B interface function has exactly the same interface, this time we need to explicitly implement the interface for processing.

When you need to call the same method signature different interfaces, the interface can be cast.

E.g:

interface IApple
{
    void Wash();
}
interface IFruit
{
    void Wash();
}
class test : IApple,IFruit
{
    void IApple.Wash()=>{};
    void IFruit.Wash()=>{};
}
test t= new test();
((IApple)t).Wash();
((IFruit)t).Wash();

This avoids conflict on the interface named.

If you are implicit and the interface implementation, all interface functions of default are sealed.
If more than a succession of problems, this may not be currently too early to talk about it, I would unhesitatingly mention, for example, the human inheritance and animals, animal bio inherited from the interface,
then for humanity, is implicitly inherited biological interfaces but for humans and animals, eating very different way, then we should override this method of eating.
We want the base class (parent class) of the virtual interface function is marked or abstract, and then be rewritten to use the override sub-category.

This has been said so far that later we will conduct in-depth analysis of the plane.

abstract

Abstract can have abstract fields, abstract classes, abstract commission, abstract functions, and so on. We have to abstract functions which are commonly used to make an analytical and abstract classes.

Abstract and interface is very similar to an abstract class that can not be instantiated, the method is not an abstract method thereof, are dependent on the subclass (inherited class) operates.

Abstract function

The interfaces and on almost exactly the same, there is no need to speak too much, if you declare a function is a function abstraction, then it method body does not exist, you need to override the subclass (override) implementation.

In practice, only a subclass of the parent class or abstract method overrides a virtual method, when no content using the method of the parent class, which is defined as an abstract method, or a method as defined virtual method.

Abstract class

"Containing one or more pure virtual functions is called an abstract class, an abstract class that can not be instantiated, further
an abstract class interface, and only by using the other as the base class."

An abstract class may contain abstract and non-abstract method, when a class inherits from the abstract class, then the derived class must implement all of
the abstract base class methods.

But by declaring the derived class is also abstract, we can avoid the realization of all or specific virtual method,
which is part of the abstract class.

It looks very deep? Indeed abstract class is a collection of functions and is provided with a specific function is implemented without some method thereof. It is nothing compared to the interface implementation, the abstract class can provide non-abstract way, that is to say, there is an abstract class may contain implementations of functions.

Here's an example

public abstract class A
{
    public void GetSomeThing()
    {
        //todo
    }
    public abstract void SetSomeThing();
}
public class B:A
{
    //实现抽象方法
    public override void SetSomeThing()
    {
        //调用非抽象方法
        base.GetSomeThing();
    }

}

This which involves the base keyword, and ":" Inherit sign in the back of inheritance, polymorphism of course there will be more in-depth introduction.

Github

BiliBili Home

WarrenRyan's Blog

Blog Park

Guess you like

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