In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how C # implements method rewriting. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
What is rewriting?
The "rewrite" parent class method is to modify its implementation or rewrite it in a subclass.
Why override the methods of the parent class?
In general, the subclass inherits the methods of the parent class, and when the object inheritance method is called, the implementation of the parent class is called and executed. But...
Yes, sometimes you need to have different implementations of inheritance methods in subclasses.
How to rewrite it?
a. Methods that rewrite the parent class use the override keyword (methods modified with the override keyword are new implementations of methods with the same name in the parent class)
b. To override the methods of the parent class, the premise is that the methods that should be overridden in the parent class must be declared as virtual, override, or abstract types, and static methods and non-virtual methods cannot be overridden. Why can override be rewritten? Because the override in the base class is actually an override of the base class of the base class, the methods of override in the base class can also be overridden because inheritance is transitive.
C. The override declaration cannot change the accessibility of the virtual method, and overriding the property declaration must specify exactly the same access modifier, type, and name as the inherited property. Adding the virtual keyword to the method to be overridden in the parent class means that its implementation can be overridden in the subclass. (note: methods in C # are not virtual by default, so you have to add the virtual keyword before they can be overridden.)
The d.virtual keyword is used to define a method that supports polymorphism. A method modified by the virtual keyword is called a virtual method (virtual method).
[access modifier] virtual [return type] method name (parameter list) {/ / implementation of virtual method, which can be overridden by subclass}
Create the console application below:
Namespace inheritDemo2 {class Employee {public virtual void EmpInfo () {Console.WriteLine ("the method modified with the virtual keyword is a virtual method");} class DervEmployee: Employee {public override void EmpInfo () {base.EmpInfo (); / / the base keyword will refer to Console.WriteLine in the following extension ("this method overrides the base method") }} class Test {static void Main (string [] args) {DervEmployee objDervEmployee = new DervEmployee (); objDervEmployee.EmpInfo (); / / Note: the instance of the objDervEmployee derived class is a reference to the objEmployee assigned to the Employee class, / / so when the objEmployee reference calls the EmpInfo () method, it still calls the method Employee objEmployee = objDervEmployee of the DervEmployee class. ObjEmployee.EmpInfo ();}
Expand:
The 1.base keyword is used to access parent class members from subclasses. Even if the methods of the parent class are overridden in the subclass, you can still use the base pass
Keyword call.
two。 Also, when you create an instance of a subclass, you can use the base keyword to call the constructor of the parent class.
3. Using the base keyword, you can only access the constructor, instance method, or instance properties of the parent class, but not the static of the base class
State method.
Thank you for reading! This is the end of the article on "how to rewrite the method of C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.