当前位置:网站首页>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*/) //析构函数
{
//...........
//初始化某些值,
}
}
边栏推荐
- GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
- [SQL] at a certain correlation with a table of data update another table
- Discourse Custom Header Links
- nSoftware.PowerShell.Server.2020
- js operation to add or subtract from the current date (day, week, month, year)
- Image stitching (registration) case based on OpenCV
- 《构建之法》笔记---第十章 典型用户和场景
- Unity beginner 5 cameras follow, border control and simple particle control (2 d)
- 05全局配置文件application.properties详解
- cnpm安装步骤
猜你喜欢

2.4希尔排序

Dynamic Programming Problems (End)

WPF study notes "WPF Layout Basics"

动态规划问题(完结篇)

MySQL String Concatenation - Various String Concatenation Practical Cases

Unity beginner 5 cameras follow, border control and simple particle control (2 d)

unity初学5 摄像机跟随,边界控制以及简单的粒子控制(2d)
![[C language] Program environment and preprocessing](/img/d2/a0437da7d651e755951dd2a15827cf.png)
[C language] Program environment and preprocessing

VisualStudio2022本地调试进入特别慢问题解决

全流程调度——Azkaban入门与进阶
随机推荐
1. Get data - requests.get()
Boss Rush (two-point answer + DP)
全流程调度——Azkaban入门与进阶
Chapter8 支持向量机
VisualStudio2022本地调试进入特别慢问题解决
1. 获取数据-requests.get()
四、Web开发
Six, read application configuration + log configuration
六、读取应用配置+日志配置
WPF study notes "WPF Layout Basics"
C. Travelling Salesman and Special Numbers (binary + combination number)
DAY17:弱口令的探测与测试
Repetition XXL - JOB scheduling center background arbitrary command execution
The Azure developer news 丨 memorabilia in July
模拟问题(上)
golang八股文整理(持续搬运)
The VUX Datetime component compute-days-function dynamically sets the date list
Introduction to database - MySQL simple introduction
Shanxi group (enterprises) in the second network security skills competition part problem WP (7)
Requirements design document and the changing role of the product manager