using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace nange_1 { class A { public int a=1;//这里和c++不一样的,c#可以再类里面给字段赋值; public int b=2; public A() { Console.WriteLine("构造函数"); } public A(int a,int b) { this.a = a; this.b = b; Console.WriteLine("构造函数"); } } class Program { static void Main(string[] args) { A objA = new A(); Console.WriteLine(objA.a+" "+ objA.b); A objB = new A { a = 3, b = 4 };//可以这样初始化对象 Console.WriteLine(objB.a + " " + objB.b); A objC = new A(5, 6); Console.WriteLine(objC.a + " " + objC.b); Console.ReadLine(); } } }输出结果如下所示:
时间: 2024-11-10 07:30:21