C#无参构造方法
/**構(gòu)造方法的作用:為屬性賦值*如果沒有顯示定義構(gòu)造方法,則會有一個默認的無參的構(gòu)造方法*如果顯示定義了構(gòu)造方法,則沒有默認構(gòu)造方法*只能用new 方法名()的形式調(diào)用構(gòu)造方法*構(gòu)造方法通常聲明為public*構(gòu)造方法沒有返回值類型*構(gòu)造方法名必須與類名相同*/
Child.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace LeiheDuixiang {/// <summary>/// 小朋友/// </summary>class Child{private string name; //字段/// <summary>/// 完成字段的對外訪問接口/// 屬性,屬性的名字首字母應該大寫/// </summary>public string Name {get { return name; }//讀訪問器,返回字段的值set { name = value; } //寫訪問器,給字段賦值}//無參的構(gòu)造方法public Child(){Name = "碧瑤";}} }Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace LeiheDuixiang {class Program{static void Main(string[] args){Child child = new Child(); //調(diào)用構(gòu)造方法Console.WriteLine("我的名字是{0}", child.Name);}} }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
總結(jié)
- 上一篇: Android蓝牙开发 — 经典蓝牙BL
- 下一篇: Codeforces Round 775