当前位置:网站首页>C - Inheritance - hidden method
C - Inheritance - hidden method
2022-07-06 07:07:00 【Tsim Sha Tsui duankun 415】
We cannot delete any member in the parent class , But you can mask the parent member with the same name as the parent member
Grammatical details :
- Mask data members - Declare members with the same name and type in subclasses
- Mask function members : Declare a new member with the same function signature in a subclass
- Let the compiler know : Add new keyword , Otherwise it will call the police
for example , The following are the fields and methods of the parent class
lass Pet
{
public string Name;
public void PrintName()
{
Console.WriteLine("Pet name is " + Name);
}
}
Then when a method with the same name is created in a subclass
class Dog:Pet
{
new public void PrintName()// added new After keyword , When called below, it calls the methods of subclasses
{
Console.WriteLine(" The dog's name is " + Name);
}
}
The printed result is not the called parent method , In this way, the hidden method can be achieved
Whether we want to call the subclass or the parent PrintName Depends on what we quote
Dog dog = new Dog
The subclass will call the subclass
Pet dog = new Dog
This call is the parent class , What you print out is Pet name is 了
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();// If it doesn't new Words , The print out here is in English
dog.Name = "jack";
dog.PrintName();
The dog's name is jack
Pet name is tom
边栏推荐
- SEO学习的最好方式:搜索引擎
- Interface automation test framework: pytest+allure+excel
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- TS基础篇
- BIO模型实现多人聊天
- LeetCode Algorithm 2181. Merge nodes between zero
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 树莓派串口登录与SSH登录方法
- Establishment and operation of cloud platform open source project environment
猜你喜欢
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
18.多级页表与快表
Entity Developer数据库应用程序的开发
Thought map of data warehouse construction
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Database basics exercise part 2
C语言_双创建、前插,尾插,遍历,删除
3. Business and load balancing of high architecture
随机推荐
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
Internal and external troubles of "boring ape" bayc
Do you really know the use of idea?
[daily question] 729 My schedule I
软件测试外包到底要不要去?三年真实外包感受告诉你
漏了监控:Zabbix对Eureka instance状态监控
Applied stochastic process 01: basic concepts of stochastic process
A brief introduction of reverseme in misc in the world of attack and defense
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
Configure raspberry pie access network
How are the open source Netease cloud music API projects implemented?
PCL realizes frame selection and clipping point cloud
SEO学习的最好方式:搜索引擎
UDP攻击是什么意思?UDP攻击防范措施
顶测分享:想转行,这些问题一定要考虑清楚!
Bio model realizes multi person chat
【每日一题】729. 我的日程安排表 I
hydra常用命令
leetcode1020. Number of enclaves (medium)
[advanced software testing step 1] basic knowledge of automated testing