C#基础之构造函数(构造器)

在每个类里面默认都有一个构造方法,正式因为有了这些方法,你未赋值的变量才会有初始值,当然,我们也可以手动自己创建构造函数,可以创建多个构造函数,自己给出默认值或者!!!规定调用此类的程序对象必须要赋值!

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Student stu=new Student(45);
 6             Console.WriteLine(stu.Age+stu.Name);
 7             Student stu2 = new Student();
 8             Console.WriteLine("======================");
 9             Console.WriteLine(stu2.Age + stu2.Name);
10         }
11         class Student
12         {
13             public Student()
14             {
15                 this.Name = "小智";
16             }
17             public Student(int age) 
18             {
19                 Age = age;
20             }
21             public int Age;
22             public string Name;
23         }
24     }

 

C#基础之构造函数(构造器)

上一篇:FastAPI 高级编程(一) 错误处理


下一篇:Node.js安装步骤Windows