Issue 1: Virtual methods
First, look at the following code.
namespacecsharptest{classA Public void Fun() {Console.WriteLine ("This is the parent class method"); } }classb:a { Public void Fun() {Console.WriteLine ("This is a subclass method"); } }classTest { Public Static voidMain () {A A =NewA (); A. Fun(); A =NewB (); A. Fun(); } }}
Guess what the result is?
If you are familiar with a friend of Java, you may think of the result:
这是父类方法这是子类方法
But actually the result is:
这是父类方法这是父类方法
This is because the class method in Java is a virtual function by default (although not in Java), the subclass function overrides the parent class's function by default (Java later provides @override annotations). However, in C #, you must use the virtual keyword to show that the function is a virtual function, and then use the Override keyword in a subclass to override the parent class method, which really implements the override of the parent class method in order to achieve polymorphism (polymorphism in C + + is implemented using virtual functions, and C # You must use the virtual keyword to display the declaration).
So, to output the result:
这是父类方法这是子类方法
The code needs to be modified as follows:
namespacecsharptest{classA PublicVirtualvoid Fun() {Console.WriteLine ("This is the parent class method"); } }classb:a { Public Override void Fun() {Console.WriteLine ("This is a subclass method"); } }classTest { Public Static voidMain () {A A =NewA (); A. Fun(); A =NewB (); A. Fun(); } }}
Add the virtual keyword to the method of the parent class and add the override keyword to the method with the same name as the child class.
Question 2:override and New keywords
First look at the following code:
namespacecsharptest{classC PublicVirtualvoid Fun() {Console.WriteLine ("This is a virtual method"); } }classC1:C { Public Override void Fun() {Console.WriteLine ("Methods using the Override keyword decoration"); } }classC2:C { Public New void Fun() {Console.WriteLine ("Methods modified with the New keyword"); } }classTest { Public Static voidMain () {C C1 =NewC1 (); C1. Fun(); C C2 =NewC2 (); C2. Fun(); } }}
Guess the above code output?
The correct result is:
使用override关键字修饰的方法这是一个虚方法
Why do you call the method of the parent class using the keyword new modifier?
Isn't it weird? Why the subclass method that uses the override keyword is called, and the subclass method using the New keyword is not called.
First look at the Override keyword: The override method provides a new implementation for members that inherit from the base class. The overridden method is called the overridden base class method, and the overridden base class method must have the same signature as the overridden method. A non-virtual or static method cannot be overridden, and the overridden base class must be virtual, abstract, or override. The override declaration cannot alter the accessibility of a virtual method, and the override method must have the same access level modifier as the virtual method. Overriding methods cannot be decorated with the following modifiers: New, static, virtual, and abstract.
New keyword: The new modifier is used to explicitly hide a member inherited from a base class. To hide inherited members, you can use the same name in the derived class and decorate it with the new modifier.
Here's an analysis of our program:
C1.fun (); Because the subclass C1 uses the override keyword to override the parent class's methods, both the base class C and the subclass C1 have the Fun () method, so C1.fun () dynamically calls the C1 's fun () method instead of the parent class.
C2.fun (); Subclass C2 uses the new keyword to hide the method of the parent class, which is equivalent to the fun () method in the subclass that inherits directly from the parent class. The fun () method declared with the New keyword in the subclass is another method, but it just happens to have the same name as the fun () method of the subclass (not a bit confusing). So C2.fun () calls the fun () method of the parent class, and if you want to call C2, you must C2 cast to C2.
Here's a look at Microsoft's official documentation explanation:
The C # language is designed so that versioning between base classes and derived classes in different libraries can evolve forward while maintaining backward compatibility. This has many meanings. For example, this means that introducing a new member in a base class with the same name as a member in a derived class is fully supported in C # and does not cause unexpected behavior. It also means that a class must explicitly declare whether a method is overriding an inherited method, or a new method that hides an inherited method with a similar name.
In C #, a derived class can contain a method with the same name as a base class method.
The base class method must be defined as virtual.
If a method in a derived class does not have a new or override keyword in front of it, the compiler will issue a warning that the method will perform as if the new keyword exists.
If a method in a derived class is preceded by a new keyword, the method is defined as independent of the method in the base class.
If the method in the derived class is preceded by the override keyword, the object of the derived class will call the method instead of calling the base class method.
The base class method can be called from a derived class using the base keyword.
The override, virtual, and new keywords can also be used in properties, indexers, and events.
By default, the C # method is a non-virtual method. If a method is declared as a virtual method, any class that inherits the method can implement its own version. To make a method a virtual method, you must use the virtual modifier in the method declaration of the base class. Derived classes can then override the base virtual method with the Override keyword, or use the New keyword to hide virtual methods in the base class. If neither the override keyword nor the new keyword is specified, the compiler issues a warning and the methods in the derived class will hide the methods in the base class.
The Override and New keywords in C #