当前位置:网站首页>C # advanced learning -- virtual method
C # advanced learning -- virtual method
2022-06-23 08:30:00 【Ambassador Tengyun】
One . Definition
To put it simply , Virtual methods are methods that can be overridden by subclasses , If the subclass overrides the virtual method , Then the runtime will use the rewritten logic , If you don't rewrite it , Then use the logic of the virtual method in the parent class .
Define keywords :virtual
Rewrite keyword :override
Two . example
Define a Person Class and Person Class Student Subclass , as follows :
public class Person
{
public string Name { get; set; }
public virtual int Age { get; set; }
public virtual void SayHello()
{
Console.WriteLine(" Hello everyone , I am a "+this.Name+" Age "+this.Age);
}
public virtual int Add(int a)
{
return this.Age+a;
}
}Suppose that the current student class has the following needs :
1. Need to be in SayHello Method to add the return of student ID
2. Age needs to be checked , When you are older than 18 At the age of , Go straight back to 18
public class Student:Person
{
public string StuNO { get; set; }
public override int Age
{
get => base.Age;
set => base.Age = value>18?18:value;
}
public override void SayHello()
{
Console.WriteLine(" Hello everyone , I am a " + this.Name+", Age "+this.Age+", My student number is "+this.StuNO);
}
}We see , For demand 1 and 2, Rewrote Age Properties and SayHello Method , Call... On the console SayHello Method , The method to invoke is as follows :
Person person = new Person(); person.Name = " people "; person.Age = 25; person.SayHello(); Student student = new Student(); student.Name = " Student "; student.Age = 25;// The age here is older than 18 student.StuNO = "001"; student.SayHello();
The results are as follows :
This is the time , We are Student Class Add Method ( It's not rewriting ), Then override the... In the parent class Add Method , as follows :
public int Add(int a,int b) // This is a new method , Indicates that the class with the same name in the parent class is overwritten , Instead of re implementing
{
return this.Age + a + 10;
}
public override int Add(int a) // This is in the overridden parent class Add Method
{
return base.Add(a)+666;
}At this time , Call... On the console Add When the method is used , If two parameters are used , Then execute the new method , If you take a parameter , Execute the override method .
If you use the method of subclass instantiating the parent class , as follows :
Person aaa = new Student(); aaa.SayHello();
Pass the verification , We will find that , At this moment SayHello The execution is overridden in a subclass SayHello Method
On the implementation order of virtual methods of subclasses and superclasses , Here is the summary of the predecessors :
1、 When calling a function of an object , The system will directly check the class defined by the object declaration , Declaration class , See if the called function is a virtual function ;
2、 If it's not a virtual function , Then it executes the function directly . And if there is virtual keyword , That is, an imaginary function , Then it won't execute the function immediately , Instead, check the instance class of the object .
3、 In this instance class , He will check whether the virtual function is re implemented in the definition of the instance class ( adopt override keyword ), If there is , that OK, It won't look for it anymore , And immediately execute the re implemented function in the instance class . And if not , The system will keep looking up for the parent class of the instance class , And repeat the check in the instance class for the parent class , Until you find the first parent class that overloads the virtual function , Then execute the overloaded function in the parent class .
thus , Conclusion of virtual method ......
Last , In fact, all data tests can be Cloud server Conduct , You can see that Tencent cloud Services related to , The server you bought as test data is very good
边栏推荐
- How to mine keywords and improve user experience before website construction?
- 正则表达式使用案例
- Deep learning ----- different methods to realize vgg16
- After easynvr video is enabled, no video file is generated. How to solve this problem?
- Install a WGet for your win10
- 4-绘制椭圆、使用定时器
- 4- draw ellipse, use timer
- 谈谈 @Autowired 的实现原理
- 开源技术交流丨批流一体数据同步引擎ChunJun数据还原-DDL功能模块解析
- INT 104_ LEC 06
猜你喜欢

jmeter压测结果分析

Qualcomm 9x07 two startup modes

Hongmeng reads the resource file

观察者模式

Talk about the implementation principle of @autowired

目标检测中的多尺度特征结合方式

Basic use of check boxes and implementation of select all and invert selection functions

81 sentences worth repeating

Introduction to typescript and basic types of variable definitions

RTSP/ONVIF协议视频平台EasyNVR启动服务报错“service not found”,该如何解决?
随机推荐
Vulnhub | dc: 4 | [actual combat]
Openvino series 19 Openvino and paddleocr for real-time video OCR processing
【论文笔记】Catching Both Gray and Black Swans: Open-set Supervised Anomaly Detection*
Install a WGet for your win10
Huawei ECS EIP cannot be pinged
Implementation of AVL tree
Crawler frame
Self organizing map neural network (SOM)
观察者模式
Multi Chain and cross chain are the future
走好数据中台最后一公里,为什么说数据服务API是数据中台的标配?
MySQL小册子笔记 5 InnoDB 记录存储结构
史上最污技术解读,60 个 IT 术语我居然秒懂了......
Fillet the tabbar with the flutter
C Scrollview scroll up or scroll down
After easynvr video is enabled, no video file is generated. How to solve this problem?
驱动架构 & platform平台总线驱动模型
最常用的5中流ETL模式
Deep learning ----- different methods to implement lenet-5 model
The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?