Let's summarize some common keywords in C:
First, use the following figure to understand public, internal, protected, and private.
Public and internal modifier classes.
Public, protected, and private modifier.
Abstract:
Abstract represents abstraction. Abstract can modify classes and methods.
ModifierThis class is called an abstract class, indicating that the modified class is incomplete and can only be used as a base class.
Abstract classes have the following features:
Abstract classes cannot be directly instantiated, and the use of the new operator for abstract classes will cause compilation errors. Although I
Some Variables and values can be abstract types during compilation, but such variables and values must be null or
References to instances that contain non-abstract classes (this non-abstract class is derived from the abstract type ).
Abstract classes cannot be sealed.
A non-abstract class (main purpose) derived from an abstract class must implement all the abstract members inherited from
And rewrite those abstract members.
ModifierIs called an abstract method. It can only be used to modify methods in abstract classes without specific implementation.
Abstract methods must be implemented using the override keyword in the derived class.
The abstract method declaration does not provide actual implementation, so there is no method body; the method declaration only uses a semicolon
End with no braces ({}) after the signature ({}). For example:
Public abstract void mymethod ();
It is incorrect to use the static or virtual modifier in the abstract method declaration, because the abstract method needs to be duplicated.
Write, so static modification is not allowed, because the abstract method is an implicit virtual method, so virtual modification is not allowed.
It is incorrect to use the abstract modifier for static attributes.
In a derived class, you can override abstract inheritance attributes by including the attribute declaration using the override modifier.
Virtual
Represents virtual, virtual, and modifies methods.
Virtual keywords are used to modify methods in the base class. Virtual instances can be used in two scenarios:
Case 1: The virtual method is defined in the base class, but the virtual method is not overwritten in the derived class. In
In an instance call, this virtual method uses the method defined by the base class.
Case 2: The virtual method is defined in the base class, and then override is used in the derived class to override the method. In
In the call to an instance of a derived class, this virtual method uses the method for deriving and rewriting.
Override
Indicates rewriting. It modifies the method.
The override method provides a new implementation of Members inherited from the base class. The method declared by override is called overwrite.
Base method. The override base method must have the same name as the override method.
When modifying the method:
1: You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or
Override.
2: The override and virtual methods must have the same access level modifier.
3: you cannot use the new, static, or virtual modifier to modify the override method.
4: to override the property declaration, you must specify the access modifier, type, and name that are exactly the same as the inherited property.
The overwritten attribute must be virtual, abstract, or override.
New
Indicates hiding. It modifies methods.
The difference between new and override is that new is to hide the parent class method, which is like telling others that this method and the parent class
There are two different methods, but their signatures are exactly the same, while override is different, override
Tell others that my method will be called using my instance in the future, and the parent class will be called using the instance of the parent class.
.
Summary: The relationship between abstract, virtual, override, and new.
The override method must be abstract, virtual, or override.
The abstract method is an implicit virtual method.
The virtual method indicates that this method can be overwritten. Of course, you can also not override it.
Abstract method indicates that this method must be overwritten.
The new method indicates that this method has nothing to do with the parent class. It is a new "new" method, just matching the signature
Same.
The above summary only deepens the understanding of the keywords in C #. Most of them refer to others. after learning the design pattern, they will use code to deepen their understanding.