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();
}
}
}
热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短