当前位置:网站首页>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
边栏推荐
- 1189. Maximum number of "balloons"
- 3. Business and load balancing of high architecture
- You deserve this high-value open-source third-party Netease cloud music player
- The best way to learn SEO: search engine
- 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
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
- Internal and external troubles of "boring ape" bayc
- Idea console color log
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- First knowledge of OpenGL es learning (1)
猜你喜欢
Establishment and operation of cloud platform open source project environment
19. Actual memory management of segment page combination
Leetcode59. spiral matrix II (medium)
What is the biggest problem that fresh e-commerce is difficult to do now
leetcode59. 螺旋矩阵 II(中等)
leetcode1020. Number of enclaves (medium)
前缀和数组系列
[daily question] 729 My schedule I
Visitor tweets about how you can layout the metauniverse
树莓派串口登录与SSH登录方法
随机推荐
升级版手机检测微信工具小程序源码-支持多种流量主模式
PCL realizes frame selection and clipping point cloud
OpenGL ES 学习初识(1)
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
UWA pipeline version 2.2.1 update instructions
LeetCode 78:子集
顶测分享:想转行,这些问题一定要考虑清楚!
Do you really know the use of idea?
RichView TRVStyle 模板样式的设置与使用
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
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
ROS learning_ Basics
Arduino tutorial - Simon games
【Hot100】739. Daily temperature
UWA Pipeline 2.2.1 版本更新说明
leetcode841. Keys and rooms (medium)
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
Top test sharing: if you want to change careers, you must consider these issues clearly!