当前位置:网站首页>C - Inheritance - polymorphism - virtual function member (lower)
C - Inheritance - polymorphism - virtual function member (lower)
2022-07-06 07:07:00 【Tsim Sha Tsui duankun 415】
Create a method , Then the parameter is our parent class Animal( When you pass it in, you should pass it to the parent class , Otherwise, if the parent class is passed in later , When calling, the subclass has some features, and the parent class does not necessarily have )
public static void Display(Animal animal)
{
System.Console.WriteLine(animal.Name);
}
And then, when called ,dog and cat All are Animal Subclasses of ,new Two objects
dog peachas = new dog();
cat egg = new cat();
When we call a method , You can pass in the subclass object
Because the subclass has all the functional features of the parent class , So parameters can be subclasses
Display(peachas);
Display(egg);
In the last article is Supplement of operators
One or two lines are equivalent to the abbreviation of the writing in the lower right corner
s This is a new variable , Give him a display conversion , This quoted s Variables can be used immediately
virtual
Marked as virual Functions of can be overridden by subclasses , Including method , Indexer , attribute , event
public class Animal
{
public string Name;
public virtual decimal Liability => 0;
}
Then the subclass should override the parent class with override Modifier
public class Egg : Animal { public long sharesOwbed; }
public class ass : Animal
{
public double mansion;
public override decimal Liability => (decimal)mansion;
}
Make a down conversion
ass mansion = new ass
{mansion = 2500 };
Animal a = mansion;
Console.WriteLine(mansion.Liability);
Console.WriteLine(a.Liability);
Both results are 2500
边栏推荐
- 中青看点阅读新闻
- 树莓派串口登录与SSH登录方法
- Call, apply, bind rewrite, easy to understand with comments
- Interface automation test framework: pytest+allure+excel
- [advanced software testing step 1] basic knowledge of automated testing
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
- First knowledge of OpenGL es learning (1)
- leetcode704. Binary search (find an element, simple, different writing)
- Entity Developer数据库应用程序的开发
- The difference between get and post request types
猜你喜欢
AI on the cloud makes earth science research easier
ROS learning_ Basics
ROS2安装及基础知识介绍
Leetcode 78: subset
Missing monitoring: ZABBIX monitors the status of Eureka instance
Internal and external troubles of "boring ape" bayc
Development of entity developer database application
19.段页结合的实际内存管理
1091: two or three things in childhood (multi instance test)
Database basics exercise part 2
随机推荐
Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
顶测分享:想转行,这些问题一定要考虑清楚!
leetcode59. 螺旋矩阵 II(中等)
呆错图床系统源码图片CDN加速与破解防盗链功能
MVVM of WPF
Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
Fast target recognition based on pytorch and fast RCNN
1189. Maximum number of "balloons"
RichView TRVStyle 模板样式的设置与使用
leetcode704. 二分查找(查找某个元素,简单,不同写法)
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
配置树莓派接入网络
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
【Hot100】739. Daily temperature
Raspberry pie serial port login and SSH login methods
Prefix and array series
PCL realizes frame selection and clipping point cloud
Hydra common commands
《从0到1:CTFer成长之路》书籍配套题目(周更)