当前位置:网站首页>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 .
边栏推荐
- What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
- Should enterprises start building progressive web applications?
- Huawei cloud micro certification Huawei cloud computing service practice has been stable
- MySQL introduction - functions (various function statistics, exercises, details, tables)
- QML add gradient animation during state transition
- JVM performance tuning and practical basic theory - medium
- How programmers find girlfriends through blind dates
- Which insurance products can the elderly buy?
- Small program graduation project based on wechat reservation small program graduation project opening report reference
- The contact data on Jerry's management device supports reading and updating operations [articles]
猜你喜欢

MySQL - use of aggregate functions and group by groups

JVM performance tuning and practical basic theory - medium

Maximum likelihood method, likelihood function and log likelihood function

Small program graduation project based on wechat e-book small program graduation project opening report function reference

MySQL deadly serial question 2 -- are you familiar with MySQL index?

Solution of cursor thickening

Gee: create a new feature and set corresponding attributes

Make drop-down menu

Magical usage of edge browser (highly recommended by program ape and student party)

Technical practice online fault analysis and solutions (Part 1)
随机推荐
Solution of cursor thickening
Rearrangement of tag number of cadence OrCAD components and sequence number of schematic page
Cancer biopsy instruments and kits - market status and future development trends
Which insurance products can the elderly buy?
Hash table, string hash (special KMP)
IPv6 experiment
Jerry's synchronous weather information to equipment [chapter]
Bacteriostatic circle scanning correction template
Future source code view -juc series
Avoid playing with super high conversion rate in material minefields
In yolov5, denselayer is used to replace focus, and the FPN structure is changed to bi FPN
Why can't it run (unresolved)
Luogu p1309 Swiss wheel
ThinkPHP uses redis to update database tables
技術實踐|線上故障分析及解决方法(上)
Force deduction solution summary 1189- maximum number of "balloons"
Feign implements dynamic URL
Three layer switching ①
Audio resource settings for U3D resource management
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]