当前位置:网站首页>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

边栏推荐
猜你喜欢

开源的网易云音乐API项目都是怎么实现的?

Internal and external troubles of "boring ape" bayc

Thought map of data warehouse construction

Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod

After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.

Raspberry pie serial port login and SSH login methods

微信公众号无限回调授权系统源码 全网首发

1091: two or three things in childhood (multi instance test)

idea控制台彩色日志

leetcode59. 螺旋矩阵 II(中等)
随机推荐
LeetCode Algorithm 2181. 合并零之间的节点
BIO模型实现多人聊天
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Leetcode 78: subset
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
从autojs到冰狐智能辅助的心里历程
leetcode59. 螺旋矩阵 II(中等)
Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
What is the difference between int (1) and int (10)? Senior developers can't tell!
leetcode1020. 飞地的数量(中等)
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
UDP攻击是什么意思?UDP攻击防范措施
树莓派串口登录与SSH登录方法
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
You deserve this high-value open-source third-party Netease cloud music player
《从0到1:CTFer成长之路》书籍配套题目(周更)
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
Huawei equipment configuration ospf-bgp linkage
Latex文字加颜色的三种办法
Compile, connect -- notes-2