static void Main(string[] args)//主方法 { Print(); Print(12); Print(12,12); Print("faaf"); Print("asdf", 15); Print(17,"sdgg"); Random rand = new Random(); int a = rand.Next(); Console.WriteLine(a); } public static void Print() { Console.WriteLine("方法的重载1"); } public static void Print(int a) { Console.WriteLine("方法的重载2"+a); } public static void Print(int a,int b) { Console.WriteLine("方法的重载3"+a+b); } public static void Print(string a) { Console.WriteLine("方法的重载4"+a); } public static void Print(string a,int b) { Console.WriteLine("方法的重载5"+a+b); } public static void Print(int a, string b) { Console.WriteLine("方法的重载6" + a + b); } }
构造函数的声明和调用: static void Main(string[] args) { person P = new person("构造函数",200); P.print(); } class person { private string name; private int age; public person(string n,int a) { name = n; age = a; } public person(string n = "构造函数带参数且有默认值", int a = 300) { name = n;//有这两句输出:构造函数,200 age = a;//没有输出: ,0(默认值) } public void print() { Console.WriteLine("{0}{1}",name,age); } }
static void Main(string[] args) { student s = new student("普通学生", 100); s.print(); student s1 = new student("普通学生", 100); s1.print(); } //静态构造函数被调用 //普通学生,100,静态构造 //普通学生,100,静态构造 class student { private static string name; private int age; private static string teachername; //public static string tN { } //public student(string n, int a,string ta) { name = n; age = a;teachername = ta; } public student(string n, int a) { name = n;age = a; } static student()//静态构造方法 { teachername = "静态构造"; Console.WriteLine("静态构造函数被调用");//只会被调用一次 } public void print() { Console.WriteLine("{0},{1},{2}", name, age, teachername); } }
特点:
static void Main(string[] args) { xigou x = new xigou(); } class xigou { public xigou() { Console.WriteLine("构造函数被调用"); }//构造函数 ~xigou(){ Console.WriteLine("析构函数被调用"); }//析构函数 }
readonl:
const:
class personn { private string name; private readonly int age = 6; private const int id = 1; public personn(string n, int a) { name = n;age = a; } public void print() { Console.WriteLine("{0},{1},{2}",name,age,id); } }