当前位置:网站首页>Override and virtual of classes in C #
Override and virtual of classes in C #
2022-07-04 01:45:00 【Andy ahui】
Welcome to be my reader , I hope this article can give you some help .
Preface
I learned class derivation with you yesterday , Know the difference between derived class and base class .
Today, let's look at how the virtual methods and override methods of derived classes and base classes are used .
When we use base class references to access derived class objects , You get members of the base class . When using virtual methods , You can get the members and functions of the derived class .
class Ahui:InfoBase
{
public string Name = " aunt ";
public string GetInfo()
{
return this.Name+""+this.Age;
}
class InfoBase
{
public int Age=27;
public string Name = "ahui"
public string GetInfo()
{
return Name;
public string Love()
{
return " Woman ";
}
}
The above code is a simple expression of base and derived classes , We try to pass the base class InfoBase To access derived classes Ahui The methods inside are shown in the output above .
static void Main(string[] args)
{
Ahui hui = new Ahui();
InfoBase info = (InfoBase)h
Console.WriteLine(hui.GetInfo());
Console.WriteLine(info.GetInfo());
Console.ReadKey();
}
From the above picture, you can see the access to... Through their respective instances GetInfo() Method returns different values , The base class returns its own , The derived class returns its own .
So when we want to use the reference of the base class to access the derived class object , Virtual methods can be used to access .
It must meet the following conditions :
- The method of the derived class and the method of the base class have the same signature and return type .
- The methods of the base class use virtual mark .
- Methods of derived classes use override mark .
class Ahui:InfoBase
{
public string Name = " aunt ";
public override string GetInfo()
{
return this.Name+""+this.Age;
}
class InfoBase
{
public int Age=27;
public string Name = "ahui" public virtual string GetInfo()
{
return Name;
public string Love()
{
return " Woman ";
}
}
Compare with the last output picture , You can see that when using virtual methods and overriding tags , All the output are members of derived classes .
It should be noted that :
- Overridden and overridden methods must have the same accessibility .
- Cannot override static Method or non virtual method .
- Method 、 Properties and indexers 、 Member type events can be declared as virtual and override.
remarks
Life is short , I don't want to pursue what I can't see , I just want to catch what I can see .
Originality is not easy. , Pay attention .
I am a A Hui , Thank you for reading , If it helps you , Please like it 、 forward thank you .
I'm glad to be friends with you .
边栏推荐
- Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
- 2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
- Huawei cloud micro certification Huawei cloud computing service practice has been stable
- LeetCode226. Flip binary tree
- How programmers find girlfriends through blind dates
- Three layer switching ②
- Msp32c3 board connection MSSQL method
- Flutter local database sqflite
- Rearrangement of tag number of cadence OrCAD components and sequence number of schematic page
- Small program graduation project based on wechat e-book small program graduation project opening report function reference
猜你喜欢
Huawei rip and BFD linkage
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
The contact data on Jerry's management device supports reading and updating operations [articles]
Install the pit that the electron has stepped on
Since the "epidemic", we have adhered to the "no closing" of data middle office services
Audio resource settings for U3D resource management
MySQL statement learning record
HackTheBox-baby breaking grad
技術實踐|線上故障分析及解决方法(上)
随机推荐
Functions and arrays of shell scripts
TP5 automatic registration hook mechanism hook extension, with a complete case
Avoid playing with super high conversion rate in material minefields
Special copy UML notes
Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]
Force buckle day32
51 MCU external interrupt
Remember a lazy query error
Huawei cloud micro certification Huawei cloud computing service practice has been stable
Rearrangement of tag number of cadence OrCAD components and sequence number of schematic page
Writeup (real questions and analysis of ciscn over the years) of the preliminary competition of national college students' information security competition
I don't know why it can't run in the project and how to change it
0 basic learning C language - nixie tube dynamic scanning display
QML add gradient animation during state transition
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
Day05 table
Small program graduation project based on wechat reservation small program graduation project opening report reference
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
[leetcode daily question] a single element in an ordered array
Long article review: entropy, free energy, symmetry and dynamics in the brain