当前位置:网站首页>Class inheritance in C #
Class inheritance in C #
2022-07-05 02:59:00 【Andy ahui】
Welcome to be my reader , I hope this article can give you some help .
Preface
In daily program coding work , Our ultimate goal is to complete the development tasks assigned by the company , The core goal is to write good code .
So what is good code ?
High cohesion , Low coupling
We must be engaged in coding , I've almost heard this sentence . So what kind of code is highly cohesive , How about low coupling .
Today, let's learn about C# In this high-level language , Through those forms .
In current high-level languages , These three characteristics are common , It is also often used in our daily coding . Namely encapsulation 、 Inherit 、 polymorphic .
Let's first look at inheritance
Class inheritance
Through inheritance, we can get a new class , The new class can define a series of methods and variables by itself , It also has the properties and behaviors of the base class .
The relationship here is base class and derived class .
It should be noted that derived classes cannot delete any members of the base class .
class Ahui:InfoBase
{
public string GetInfo()
{
return this.Name+""+this.Age;
}
}
class InfoBase
{
public int Age { get; set; }
public int Name { get; set; }
public string Love()
{
return " Woman ";
}
}
The above example ,InfoBase It belongs to the base class ,Ahui It belongs to a derived class , You can see that inheritance is achieved by colons .
Inherited members can be accessed , Just as they are declared by the derived class itself .
Currently, all classes are derived from object class ,object Class is the only non derived class , It is the basis of inheritance hierarchy . Classes that currently have no base class specification are implicitly derived directly from classes object.
matters needing attention
1、 There can only be one separate class in the base class specification of a class declaration , It is called single inheritance .
2、 Although a class can only directly inherit one base class , But there is no limit to the level of inheritance .
At present, derived classes cannot delete members and functions of base classes , But you can shield base class members and functions by declaring the same members in derived classes . Note here that the names must be the same .
class Program
{
static void Main(string[] args)
{
Ahui hui = new Ahui();
Console.WriteLine(hui.Name);
Console.ReadKey();
}
}
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 Love()
{
return " Woman ";
}
}
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 .
边栏推荐
- Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
- Scientific research: are women better than men?
- Tiny series rendering tutorial
- Design and implementation of community hospital information system
- Apache Web page security optimization
- Acwing game 58 [End]
- Sqoop命令
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- 1. Five layer network model
- Design and implementation of kindergarten management system
猜你喜欢
Acwing game 58 [End]
SQL performance optimization skills
【LeetCode】222. The number of nodes of a complete binary tree (2 mistakes)
Acwing第 58 场周赛【完结】
Pat grade a 1119 pre- and post order traversals (30 points)
Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics
Sqoop安装
openresty ngx_lua执行阶段
Azkaban overview
8. Commodity management - commodity classification
随机推荐
Hmi-31- [motion mode] solve the problem of picture display of music module
Acwing第 58 场周赛【完结】
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
LeetCode146. LRU cache
El select, El option drop-down selection box
Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
Azkaban installation and deployment
Pytest (4) - test case execution sequence
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
Zabbix
腾讯云,实现图片上传
Devtools的簡單使用
Scientific research: are women better than men?
100 basic multiple choice questions of C language (with answers) 04
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan
How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
Sqoop command
SPI and IIC communication protocol
打破信息茧房-我主动获取信息的方法 -#3
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety