using System; class A //所有类都继承自: Object ...{ public A()//继承自基类的构函数:base() ...{ Console.WriteLine("轻轻的我来了!"); } } class Test ...{ staticvoid Main() ...{ A a =new A();//A()是调用构造器 } }
类实例化时为类的 成员变量赋初值
using System; class A //所有类都继承自: Object ...{ publicint i; publicstring s; public A()//继承自基类的构函数:base() ...{ Console.WriteLine("构造函数的值"); } } class Test ...{ staticvoid Main() ...{ A a =new A(); Console.WriteLine("i="+ a.i); Console.WriteLine("i="+ a.s); } }
构造器综合实例
using System; class A //所有类都继承自: Object ...{ publicint i; publicstring s; public A()//继承自基类的构函数:base() ...{ i =100; s ="书本"; } public A(int a):this() ...{ i = a; } public A(string b): this() ...{ s = b; } public A(int a, string b) : this() ...{ i=a; s = b; } } class Test ...{ staticvoid Main() ...{ A a =new A(); Console.WriteLine("第一个构造器"); Console.WriteLine("i="+ a.i); Console.WriteLine("s="+ a.s); A a1 =new A(11111); Console.WriteLine("第二个构造器"); Console.WriteLine("i="+ a1.i); Console.WriteLine("s="+ a1.s); A a2 =new A("我我我我我"); Console.WriteLine("第三个构造器"); Console.WriteLine("i="+ a2.i); Console.WriteLine("s="+ a2.s); A a3 =new A(11111,"我我我我我"); Console.WriteLine("第四个构造器"); Console.WriteLine("i="+ a3.i); Console.WriteLine("s="+ a3.s); } }
using System;class A //所有类都继承自: Object...{ public A()//继承自基类的构函数:base() ...{ Console.WriteLine("轻轻的我来了!"); }}class Test...{ static void Main() ...{ A a = new A();//A()是调