Keywords used to modify classes, methods, properties, and fields:
First, explain it from the simplest private, protected, internal, public.
public and internal modification classes.
public, protected, private modification method.
When modifying:
public stands for public, that is, all assembly can access this class.
internal represents internal, that is, this class can only be accessed in the same assembly. Generally speaking, the same assembly is the same DLL.
When modifying:
public stands for public, that is, all classes can access this method.
protected means protected, that is, only my subclass can access this method.
private stands for private, that is, only I can access this method.
Next, let's discuss the difference between const and readonly:
First of all, both are constants. The difference is that const is a compile-time constant and readonly is a run-time constant.
Compilation-time constant: When compiling, the value is already a constant, and anywhere using the variable will be replaced with the constant value.
Runtime constant: During runtime, the value cannot be modified.
public class Test
{
public const string constStr = "this is a test";
public readonly string readonlyStr = "this can't be modified in runtime time";
public void Method1()
{
string s1 = constStr; // When compiling, this sentence will be replaced with string s1 = "this is a test";
readonlyStr = "error";
//Try to modify readonlyStr, but it cannot be compiled.
//Top an error: The readonly field can only be modified when constructor and initialized.
}
}
OK, now we will discuss abstract, virtual, new, override keywords:
First of all, abstract represents abstract, and abstract can modify classes and methods.
When modifying:
This is called an abstract class, and abstract class has the following properties:
Abstract classes cannot be instantiated.
An abstract class can contain abstract methods and abstract accessors, and the accessor is actually a method.
The abstract class cannot be modified with the sealed modifier because the meaning of these two modifiers is opposite. Classes that use sealed modifiers cannot be inherited, while abstract modifiers require inheritance of classes.
Non-abstract classes derived from abstract classes must include all inherited abstract methods and actual implementations of abstract accessors.
When modifying the method:
This is called an abstract method, and its properties are as follows:
Abstract methods are implicit virtual methods (virtual modification methods are called virtual methods).
Only abstract method declarations are allowed in abstract classes. As long as abstract methods are used, this is an abstract class.
Because abstract method declarations do not provide actual implementations, there is no method body; method declarations simply end with a semicolon and do not have braces ({ }) after signature. For example:
public abstract void MyMethod();
The implementation is provided by an override method override, which is a member of a non-abstract class.
It is wrong to use static or virtual modifiers in abstract method declarations. Because abstract methods need to be rewritten, they cannot be modified with static because abstract methods are implicit virtual methods, so they cannot be modified with virtual.
Except that it is different in declaration and call syntax, abstract properties behave like abstract methods, which are essentially methods.
It is wrong to use the abstract modifier on static properties.
In derived classes, abstract inherited properties can be overridden by including attribute declarations using the override modifier.
The virtual keyword represents virtual, virtual, and the method is modified.
When modifying:
This is called a virtual method. The virtual method means that this method is virtual. This method may not be implemented, and this method can be rewritten.
The key sentence is: This method can be rewritten.
This means that if this method wants to be overridden and override, then it must be a virtual method, because the abstract modified method is an implicit virtual method, so the abstract and virtual modified methods can be override.
The override keyword represents rewriting, and the modification is the method.
The override method provides a new implementation of members inherited from the base class. The method rewritten by override is called the rewrite base method. The overridden base method must have the same signature as the override method.
When modifying the method:
1: Non-virtual methods or static methods cannot be rewrite. The overridden base method must be virtual, abstract, or override.
The override method and the virtual method must have the same access level modifier.
2: You cannot use new, static, or virtual modifiers to modify the override method.
3: Rewrite attribute declaration must specify the exact same access modifier, type, and name as inherited attribute, and the rewritten attribute must
Is virtual, abstract or override.
The new keyword represents hidden, and the modification is the method.
The difference between new and override is that new hides the parent class method. This is like telling others that this method and the parent class method are two different methods, but their signatures are exactly the same, and override is different. override tells others that in the future, my method is called with my instance, and the parent class method is called with the instance of the parent class.
To summarize:abstract,virtual,override,new relationship.
The method of override must be abstract , virtual , override .
The abstract method is an implicit virtual method.
The virtual method means that this method can be rewritten, and of course you can not rewrite it.
The abstract method means that this method is a method that must be rewritten.
The new method means that this method has nothing to do with the parent class. It is a new "new" method, but it just happens to have the same signature.
The last question:
class A
{
public virtual void F()
{
("");
}
}
class B : A
{
public override void F()
{
("");
}
}
class C : B
{
new public virtual void F() { (""); }
}
class D : C
{
public override void F() { (""); }
}
class Program2
{
static void Main()
{
D d = new D();
A a = d; B b = d;
C c = d; (); ();
();
();
}
}
First, explain it from the simplest private, protected, internal, public.
public and internal modification classes.
public, protected, private modification method.
When modifying:
public stands for public, that is, all assembly can access this class.
internal represents internal, that is, this class can only be accessed in the same assembly. Generally speaking, the same assembly is the same DLL.
When modifying:
public stands for public, that is, all classes can access this method.
protected means protected, that is, only my subclass can access this method.
private stands for private, that is, only I can access this method.
Next, let's discuss the difference between const and readonly:
First of all, both are constants. The difference is that const is a compile-time constant and readonly is a run-time constant.
Compilation-time constant: When compiling, the value is already a constant, and anywhere using the variable will be replaced with the constant value.
Runtime constant: During runtime, the value cannot be modified.
Copy the codeThe code is as follows:
public class Test
{
public const string constStr = "this is a test";
public readonly string readonlyStr = "this can't be modified in runtime time";
public void Method1()
{
string s1 = constStr; // When compiling, this sentence will be replaced with string s1 = "this is a test";
readonlyStr = "error";
//Try to modify readonlyStr, but it cannot be compiled.
//Top an error: The readonly field can only be modified when constructor and initialized.
}
}
OK, now we will discuss abstract, virtual, new, override keywords:
First of all, abstract represents abstract, and abstract can modify classes and methods.
When modifying:
This is called an abstract class, and abstract class has the following properties:
Abstract classes cannot be instantiated.
An abstract class can contain abstract methods and abstract accessors, and the accessor is actually a method.
The abstract class cannot be modified with the sealed modifier because the meaning of these two modifiers is opposite. Classes that use sealed modifiers cannot be inherited, while abstract modifiers require inheritance of classes.
Non-abstract classes derived from abstract classes must include all inherited abstract methods and actual implementations of abstract accessors.
When modifying the method:
This is called an abstract method, and its properties are as follows:
Abstract methods are implicit virtual methods (virtual modification methods are called virtual methods).
Only abstract method declarations are allowed in abstract classes. As long as abstract methods are used, this is an abstract class.
Because abstract method declarations do not provide actual implementations, there is no method body; method declarations simply end with a semicolon and do not have braces ({ }) after signature. For example:
public abstract void MyMethod();
The implementation is provided by an override method override, which is a member of a non-abstract class.
It is wrong to use static or virtual modifiers in abstract method declarations. Because abstract methods need to be rewritten, they cannot be modified with static because abstract methods are implicit virtual methods, so they cannot be modified with virtual.
Except that it is different in declaration and call syntax, abstract properties behave like abstract methods, which are essentially methods.
It is wrong to use the abstract modifier on static properties.
In derived classes, abstract inherited properties can be overridden by including attribute declarations using the override modifier.
The virtual keyword represents virtual, virtual, and the method is modified.
When modifying:
This is called a virtual method. The virtual method means that this method is virtual. This method may not be implemented, and this method can be rewritten.
The key sentence is: This method can be rewritten.
This means that if this method wants to be overridden and override, then it must be a virtual method, because the abstract modified method is an implicit virtual method, so the abstract and virtual modified methods can be override.
The override keyword represents rewriting, and the modification is the method.
The override method provides a new implementation of members inherited from the base class. The method rewritten by override is called the rewrite base method. The overridden base method must have the same signature as the override method.
When modifying the method:
1: Non-virtual methods or static methods cannot be rewrite. The overridden base method must be virtual, abstract, or override.
The override method and the virtual method must have the same access level modifier.
2: You cannot use new, static, or virtual modifiers to modify the override method.
3: Rewrite attribute declaration must specify the exact same access modifier, type, and name as inherited attribute, and the rewritten attribute must
Is virtual, abstract or override.
The new keyword represents hidden, and the modification is the method.
The difference between new and override is that new hides the parent class method. This is like telling others that this method and the parent class method are two different methods, but their signatures are exactly the same, and override is different. override tells others that in the future, my method is called with my instance, and the parent class method is called with the instance of the parent class.
To summarize:abstract,virtual,override,new relationship.
The method of override must be abstract , virtual , override .
The abstract method is an implicit virtual method.
The virtual method means that this method can be rewritten, and of course you can not rewrite it.
The abstract method means that this method is a method that must be rewritten.
The new method means that this method has nothing to do with the parent class. It is a new "new" method, but it just happens to have the same signature.
The last question:
Copy the codeThe code is as follows:
class A
{
public virtual void F()
{
("");
}
}
class B : A
{
public override void F()
{
("");
}
}
class C : B
{
new public virtual void F() { (""); }
}
class D : C
{
public override void F() { (""); }
}
class Program2
{
static void Main()
{
D d = new D();
A a = d; B b = d;
C c = d; (); ();
();
();
}
}