[C#學(xué)習(xí)筆記12]實(shí)例方法多種形式講解與靜態(tài)方法、方法重載
方法定義規(guī)范:
訪問修飾符?返回值類型?方法名(參數(shù)1,參數(shù)2......)
{
????//功能實(shí)現(xiàn)
}
調(diào)用規(guī)范:對象名.方法名(參數(shù)1,參數(shù)2......);
注意事項(xiàng):訪問修飾符默認(rèn)private,不建議省略。根據(jù)需要定義成public
????????方法名用Pascal命名法,首字母大寫。動(dòng)詞或動(dòng)賓短語
????????有返回值用return返回,return后不能跟其他語句。無返回值使用void。
public class Student
? ? {
//構(gòu)造方法
? ? ? ? /// <summary>
? ? ? ? ///默認(rèn)的構(gòu)造方法:如果不寫,會(huì)自動(dòng)生成
? ? ? ? /// </summary>
? ? ? ? public Student()
? ? ? ? { }
? ? ? ? public Student(int studentId, string studentName)
? ? ? ? {
? ? ? ? ? ? this.StudentId = studentId;
? ? ? ? ? ? this.StudentName = studentName;
? ? ? ? }
? ? ? ? public Student(int studentId, string studentName, DateTime dateOfBirth)
? ? ? ? ? ? : this(studentId, studentName)
? ? ? ? {
? ? ? ? ? ? this.DateOfBirth = dateOfBirth;
? ? ? ? }
//屬性
? ? ? ? public int StudentId { get; set; }
? ? ? ? public string StudentName { get; set; }
? ? ? ? public DateTime DateOfBirth { get; set; }
? ? ? ? public int Age
? ? ? ? {
? ? ? ? ? ? get { return DateTime.Now.Year - DateOfBirth.Year; }
? ? ? ? }
? ? ? ? //還可以使用屬性表達(dá)式(更簡潔)
? ? ? ? public int StudentAge => DateTime.Now.Year - DateOfBirth.Year;
?//?實(shí)例方法的5種形式
? ? ? ? /// <summary>
? ? ? ? /// 【1】沒有返回值,沒有參數(shù)的方法
? ? ? ? /// </summary>
? ? ? ? public void GetVoidStudent()
? ? ? ? {
? ? ? ? ? ? string info = $"姓名:{StudentName}? 學(xué)號:{StudentId}";
? ? ? ? ? ? Console.WriteLine("這個(gè)是第1個(gè)方法:沒有返回值,沒有參數(shù)\r\n----------------------");
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 【2】沒有返回值,有參數(shù)
? ? ? ? /// </summary>
? ? ? ? /// <param name="studentName"></param>
? ? ? ? /// <param name="studentId"></param>
? ? ? ? public void GetVoidStudent(string studentName, int studentId)
? ? ? ? {
? ? ? ? ? ? string info = $"姓名:{studentName}? 學(xué)號:{studentId}";
? ? ? ? ? ? Console.WriteLine(info);
? ? ? ? ? ? Console.WriteLine("這個(gè)是第2個(gè)方法:沒有返回值,有參數(shù)\r\n--------------------------");
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 【3】有返回值、沒有參數(shù)
? ? ? ? /// </summary>
? ? ? ? /// <returns></returns>
? ? ? ? public string GetStringStudent()
? ? ? ? {
? ? ? ? ? ? string info = $"姓名:{StudentName}? 學(xué)號:{StudentId}";
? ? ? ? ? ? Console.WriteLine("這個(gè)是第3個(gè)方法:有返回值,沒有參數(shù)\r\n--------------------------");
? ? ? ? ? ? return info;
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 【4】有返回值,有參數(shù)
? ? ? ? /// </summary>
? ? ? ? /// <param name="studentName"></param>
? ? ? ? /// <param name="studentId"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public string GetStringStudent(string studentName, int studentId)
? ? ? ? {
? ? ? ? ? ? string info = $"姓名:{studentName}? 學(xué)號:{studentId}";
? ? ? ? ? ? Console.WriteLine("這個(gè)是第4個(gè)方法:有返回值,有參數(shù)\r\n-----------------------------");
? ? ? ? ? ? //如果去掉最后一個(gè)else,會(huì)出現(xiàn)返回值錯(cuò)誤問題
? ? ? ? ? ? if (studentId == 10001)
? ? ? ? ? ? ? ? return info;
? ? ? ? ? ? else if (studentId == 10002)
? ? ? ? ? ? ? ? return info + ":10002";
? ? ? ? ? ? else
? ? ? ? ? ? ? ? return info + ":10000";
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 【5】參數(shù)帶默認(rèn)值的方法
? ? ? ? /// </summary>
? ? ? ? /// <param name="studentId"></param>
? ? ? ? /// <param name="studentName"></param>
? ? ? ? /// <returns></returns>
? ? ? ? public string GetStudent(int studentId, string studentName = "匿名學(xué)員")
? ? ? ? {
? ? ? ? ? ? string info = $"姓名:{studentName}? 學(xué)號:{studentId}";
? ? ? ? ? ? Console.WriteLine("這個(gè)是第5個(gè)方法:參數(shù)帶默認(rèn)值的方法\r\n-------------------------");
? ? ? ? ? ? return info;
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 靜態(tài)方法:(調(diào)用的時(shí)候是通過類名.方法名稱調(diào)用的)
? ? ? ? /// </summary>
? ? ? ? /// <returns></returns>
? ? ? ? public static string[] GetCourseList()
? ? ? ? {
? ? ? ? ? ? //我們可以從數(shù)據(jù)庫中獲取
? ? ? ? ? ? string[] courseArray = { ".Net課程", ".Net/C#課程", ".NET高級進(jìn)階VIP課程", "課程" };
? ? ? ? ? ? string[] courseInfo = new string[courseArray.Length];
? ? ? ? ? ? for (int i = 0; i < courseArray.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? courseInfo[i] = $"第{i + 1}門課程名稱:{courseArray[i]}";
? ? ? ? ? ? }
? ? ? ? ? ? return courseInfo;
? ? ? ? }
? ? }
? ? class TestClass2
? ? {
? ? ? ? public void Dowork1()
? ? ? ? {
? ? ? ? ? ? Student student = new Student()
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DateOfBirth = Convert.ToDateTime("1996-09-10"),
? ? ? ? ? ? ? ? StudentId = 1003,
? ? ? ? ? ? ? ? StudentName = "課程學(xué)員小李"
? ? ? ? ? ? };
? ? ? ? ? ? //【1】調(diào)用沒有返回值、沒有參數(shù)的方法
? ? ? ? ? ? student.GetVoidStudent();
? ? ? ? ? ? //【2】調(diào)用沒有返回值的、有參數(shù)的方法?
? ? ? ? ? ? student.GetVoidStudent("新學(xué)員小趙", 20001);
? ? ? ? ? ? //【3】調(diào)用有返回值、沒有參數(shù)的方法? ? ? ? ? ?
? ? ? ? ? ? string result = student.GetStringStudent();
? ? ? ? ? ? Console.WriteLine(result);
? ? ? ? ? ? //【4.1】調(diào)用有返回值、有參數(shù)的方法? ?
? ? ? ? ? ? result = student.GetStringStudent("老學(xué)員小張", 20002);
? ? ? ? ? ? Console.WriteLine(result);
? ? ? ? ? ? //【4.2】使用《命名參數(shù)》(調(diào)用的時(shí)候使用)
? ? ? ? ? ? // result = student.GetStringStudent(20002, "老學(xué)員小張");//這個(gè)方法調(diào)用參數(shù)是錯(cuò)誤的
? ? ? ? ? ? result = student.GetStringStudent(studentId: 20002, studentName: "上位機(jī)學(xué)員李四");
? ? ? ? ? ? //【5.1】有返回值、有參數(shù)(參數(shù)使用默認(rèn)值)
? ? ? ? ? ? Console.WriteLine("這個(gè)是帶默認(rèn)值參數(shù)的方法");
? ? ? ? ? ? result = student.GetStudent(20003);
? ? ? ? ? ? Console.WriteLine(result);
? ? ? ? ? ? //【5.2】默認(rèn)值也可以有參數(shù)?
? ? ? ? ? ? result = student.GetStudent(2004, "我的新學(xué)員小浩");
? ? ? ? ? ? Console.WriteLine(result);
? ? ? ?}
? ? ? ? public void Dowork2()
? ? ? ? {
? ? ? ? ? ? //靜態(tài)方法的調(diào)用
? ? ? ? ? ? Console.WriteLine("展示一下我報(bào)名的課程:");
? ? ? ? ? ? string[] course = Student.GetCourseList();
? ? ? ? ? ? foreach (var item in course)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(item);
? ? ? ? ? ? }
? ? ? ? }
? ? }
//構(gòu)造方法的使用
? ? ? ? ? ? TestClass1 testClass1 = new TestClass1();
? ? ? ? ? ? testClass1.Dowork1();
? ? ? ? ? ? testClass1.Dowork2();
? ? ? ? ? ? TestClass2 testClass2 = new TestClass2();
? ? ? ? ? ? testClass2.Dowork1();
? ? ? ? ? ? testClass2.Dowork2();
方法重載
????方法重載優(yōu)點(diǎn):減少累的對外接口,只顯示一個(gè)方法,降低類的復(fù)雜度
????????????????????便于用戶的使用和識別,相同功能的方法名一樣
????方法重載條件:方法名稱必須一樣;方法的參數(shù)個(gè)數(shù)或類型不一樣
????方法重載與返回值無關(guān)
static修飾后稱為靜態(tài)類、靜態(tài)方法、靜態(tài)字段
????靜態(tài)方法的調(diào)用:類名.方法名
????靜態(tài)成員的使用
????????????靜態(tài)成員在程序運(yùn)行時(shí)被調(diào)入內(nèi)存中,在系統(tǒng)未關(guān)閉之前不會(huì)被GC回收;類成員使用頻繁時(shí),可以使用static修飾,不要使用過多;靜態(tài)成員不能直接調(diào)用實(shí)例成員,靜態(tài)方法不能直接調(diào)用實(shí)例方法;靜態(tài)方法也可以重載。