C#类的创建与初始化实例解析
本文以一个实例简单实现了类的创建与初始化,实现代码如下所示:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceC_program_test
{
classPerson
{
publicstringName;//因为Name没有赋值,而它是string类型的,所以它的默认值就是Null
publicintAge;//因为Age和Gender也没有赋值,而它两是int类型的,所以他们两个的默认值为0
publicintGender;
publicvoidsayHello()
{
Console.WriteLine("大家好");
Console.ReadKey();
}
}
classProgram
{
staticvoidMain(string[]args)
{
//inti=1;//值类型初始化就是直接赋值。而引用类型初始化就需要new
Personp1=newPerson();//newPerson()就是创建一个Person类对象。Personp1=newPerson()就是指先创建一个Person类型的对象然后用变量p1指向它
p1.sayHello();
}
}
}
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下