本文以一個實例簡單實現了類的創建與初始化,實現代碼如下所示:
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace C_program_test{ class Person { public string Name; //因為Name沒有賦值,而它是string類型的,所以它的默認值就是Null public int Age; //因為Age和Gender也沒有賦值,而它兩是int類型的,所以他們兩個的默認值為0 public int Gender; public void sayHello() { Console.WriteLine("大家好"); Console.ReadKey(); } } class Program { static void Main(string[] args) { //int i = 1;// 值類型初始化就是直接賦值。而引用類型初始化就需要new Person p1 = new Person(); //new Person()就是創建一個Person類對象。Person p1 = new Person()就是指先創建一個Person類型的對象 然后用變量p1指向它 p1.sayHello(); } }}
新聞熱點
疑難解答