当前位置:网站首页>C# One Week Introductory "C# - Classes and Objects" Day Six
C# One Week Introductory "C# - Classes and Objects" Day Six
2022-07-30 04:42:00 【solar storm】
导言:Go through the knowledge of classes and objects again here,
一、类和对象
1. 类是什么?
A class is a syntactic representation used to describe objects in the real world,It is an abstraction of the human mind.
2. 对象是什么?
The object here mainly refers to the use of computer language to describe things in reality.is the object that will be instantiated.
3. 类与对象的关系
A real object necessarily falls under our human classification,有很多属性,Take people, for example,A person has many attributes,有年龄、姓名、性别等等,在c#You can use classes in it、结构体、etc. syntax to describe the information that represents a person,But people don't just have attributes,还有行为,Like being able to sing,able to dance etc.Structs are obviously inconvenient to describe behavior,Structures mainly store data,It just so happens that the class has the function of describing the behavior of the object with functions,So classes are the best practice for describing objects.
二、C#类语法
1. 类的语法
类语法 使用 class 关键字 和 statement block to describe,Declare a class instance as follows
public clsss Person
{
string Name;
int Age;
bool Sex;
public void Dance()
{
//跳舞
Console.WriteLine("I Can Dance!");
}
public void Sing()
{
//唱歌
Console.WriteLine("I Can Sing!");
}
}
How to use a class to generate an object?
使用 new 关键字即可
Person zhangShan = new Person();
zhangShan.Dance();
//zhangShanThis subject has just performed a dance
- 没有默认构造函数 会自动生成构造函数
- To access objects using is “.” symbolic access
2. 类的访问权限
类的默认访问标识符是 internal,成员的默认访问标识符是 private.These symbols govern classes、Class member accessors
The general class of access I generally write is public
Let's briefly discuss it 类成员的访问 标识符
- public:The instantiated object can be accessed
- private:The instantiated object cannot be accessed
public clsss Person
{
public string Name = "张珊";
public int Age;
private bool Sex;
public Person()
{
}
public void Dance()
{
//跳
Console.WriteLine("I Can Dance!");
}
public void Sing()
{
//唱
Console.WriteLine("I Can Sing!");
}
private void Rap()
{
//Rap
Console.WriteLine("I Can Rap!");
}
}
我们在使用时,Know what the following situations mean
Person zhangShan = new Person();
//正确
zhangShan.Dance();
zhangShan.Sing();
var name = zhangShan.Name;
var age = zhangShan.Age;
//错误-不能访问
zhangShan.Rap();
zhangShan.Sex();
3. 特殊函数
1、构造函数
类的 构造函数 是类的一个特殊的成员函数,当创建类的新对象时执行.也就是只执行一次
构造函数的名称与类的名称完全相同,它没有任何返回类型.
public clsss Person
{
//............
public Person(/*可跟参数*/)
{
//...........
//初始化某些值,
}
}
2、析构函数
类的 析构函数 是类的一个特殊的成员函数,当类的对象超出范围时执行.That is to say, we do not automatically call、Also do not call manually
析构函数的名称是在类的名称前加上一个波浪形(~)作为前缀,它不返回值,也不带任何参数.
析构函数用于在结束程序(比如关闭文件、释放内存等)之前释放资源,
```csharp
public clsss Person
{
//............
public Person(/*可跟参数*/) //构造函数
{
//...........
//初始化某些值,
}
~Person(/*Parameters cannot be followed*/) //析构函数
{
//...........
//初始化某些值,
}
}
边栏推荐
- Image stitching (registration) case based on OpenCV
- Code open source design and implementation ideas
- "Translation" Envoy Fundamentals, this is a training course, make people to more quickly using Envoy Proxy..
- Shi Xingguo, founder of Hyperchain, was interviewed by 21st Century Business Herald to interpret Shanghai's new NFT regulations and digital development
- KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!
- MySQL installation error solution
- 需求设计文档和产品经理的角色改变
- Catch That Cow(详解)
- cnpm installation steps
- labelme的使用技巧
猜你喜欢
[Redis Master Cultivation Road] Jedis - the basic use of Jedis
Code readability, pre-checks, comments and summaries
DAY17、CSRF 漏洞
handler+message [message mechanism]
Android Studio implements login registration - source code (connecting to MySql database)
【线性表】- LeetCode力扣三道练习题详解
Image stitching (registration) case based on OpenCV
2.5快速排序
cnpm installation steps
Seven, custom configuration
随机推荐
C. Travelling Salesman and Special Numbers (binary + combination number)
《构建之法》笔记---第十章 典型用户和场景
cnpm安装步骤
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!
- B + tree index and MySQL series 】 【 what is the difference between a HASH index
The Complete Go Books - Beginner to Advanced and Web Development
Alibaba Cloud's EasyNLP Chinese text image generation model takes you to become an artist in seconds
复现XXL-JOB 任务调度中心后台任意命令执行漏洞
Shanxi group (enterprises) in the second network security skills competition part problem WP (8)
字符串问题(上)
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
[Redis Master Cultivation Road] Jedis - the basic use of Jedis
Become a qualified cybersecurity, do you know this?
3. Dependency configuration management
Simple experiment with BGP
解决go环境编译不了exe
error: The following untracked working tree files would be overwritten by
WPF recursively obtains the list of specified control types in the form
How with Mexico Volkswagen VW EDI connection to Mexico