当前位置:网站首页>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 .
边栏推荐
- 1189. Maximum number of "balloons"
- Summary of common tools and technical points of PMP examination
- Solution to the problem that jsp language cannot be recognized in idea
- Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
- Why can't it run (unresolved)
- Gee: create a new feature and set corresponding attributes
- Cancer biopsy instruments and kits - market status and future development trends
- Who moved my code!
- 2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
- Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
猜你喜欢

Openbionics exoskeleton project introduction | bciduino community finishing

1189. Maximum number of "balloons"

ES6 deletes an attribute in all array objects through map, deconstruction and extension operators

JVM performance tuning and practical basic theory - medium

Solution of cursor thickening

Conditional statements of shell programming

Huawei rip and BFD linkage

Huawei BFD and NQA

Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference

Hbuilder link Xiaoyao simulator
随机推荐
Avoid playing with super high conversion rate in material minefields
Three layer switching ②
7.1 learning content
2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
Description of setting items of Jerry [chapter]
Gee: create a new feature and set corresponding attributes
Luogu p1309 Swiss wheel
How to delete MySQL components using xshell7?
Small program graduation design is based on wechat order takeout small program graduation design opening report function reference
Douban scoring applet Part-3
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
Customize redistemplate tool class
The contact data on Jerry's management device supports reading and updating operations [articles]
Logical operator, displacement operator
Containerization technology stack
Conditional test, if, case conditional test statements of shell script
Special copy UML notes
JVM performance tuning and practical basic theory - medium
Portable two-way radio equipment - current market situation and future development trend
I don't know why it can't run in the project and how to change it