当前位置:网站首页>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 .
边栏推荐
- Fundamentals of machine learning: feature selection with lasso
- Technical practice online fault analysis and solutions (Part 1)
- From the 18th line to the first line, the new story of the network security industry
- Feign implements dynamic URL
- Force deduction solution summary 1189- maximum number of "balloons"
- C import Xls data method summary III (processing data in datatable)
- Solution of cursor thickening
- File contains vulnerability summary
- HackTheBox-baby breaking grad
- How to view the computing power of GPU?
猜你喜欢
[turn] solve the problem of "RSA public key not find" appearing in Navicat premium 15 registration
Bacteriostatic circle scanning correction template
Yyds dry goods inventory it's not easy to say I love you | use the minimum web API to upload files
What are the advantages and disadvantages of data center agents?
AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
MySQL introduction - functions (various function statistics, exercises, details, tables)
MPLS③
Lightweight Pyramid Networks for Image Deraining
Conditional test, if, case conditional test statements of shell script
Small program graduation project based on wechat reservation small program graduation project opening report reference
随机推荐
LeetCode226. Flip binary tree
It's corrected. There's one missing < /script >, why doesn't the following template come out?
Day05 branch and loop (II)
Jerry's synchronous weather information to equipment [chapter]
Solution of cursor thickening
Luogu p1309 Swiss wheel
MPLS③
Neo4j learning notes
C import Xls data method summary I (upload files and create Workbooks)
MySQL introduction - functions (various function statistics, exercises, details, tables)
Some other configurations on Huawei's spanning tree
Pesticide synergist - current market situation and future development trend
String hash, find the string hash value after deleting any character, double hash
Software product download collection
About uintptr_ T and IntPtr_ T type
I don't care about you. OKR or KPI, PPT is easy for you
Introduction to superresolution
Logical operator, displacement operator
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
Special copy UML notes