簡單類及成員實例(C#)
題目描述
簡單類及成員實例。定義了如下圖所示類Student,根據下圖和給出代碼,補寫缺失的代碼。
using System;
namespace sample{
? ? class Student {
? ? ? ? public string studentid;//學號
? ? ? ? public string studentname;//姓名
? ? ? ? private string birthplace;//籍貫
? ? ? ? private DateTime birthdate;//出生日期
? ? ? ? /
? ? ? ? //請填寫代碼,實現類的無參和有參構造函數、
? ? ? ? //屬性StudentId、StudentName、BirthPlace、BirthDate、Age
? ? ? ? /
? ? }
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Student zs = new Student("201753501234", "zs");
? ? ? ? ? ? zs.BirthDate = DateTime.Parse("1988-12-10");
? ? ? ? ? ? zs.BirthPlace = "jinan";
? ? ? ? ? ? string s = "name:{0},no:{1},native:{2},age:{3}";
? ? ? ? ? ? Console.WriteLine(s,zs.StudentName,zs.StudentId,zs.BirthPlace,zs.Age);
? ? ? ? }
? ? }
}
輸入
無
輸出
輸出姓名、學號、籍貫、年齡等信息
樣例輸入
copy
無
樣例輸出
name:zs,no:201753501234,native:jinan,age:33
提示
1、年齡Age是只讀屬性,
2、學號StudentId、姓名StudentName、出生日期BirthDate、籍貫BirthPlace為一般屬性;
3、構造函數有無參和有參兩種;
public Student(string s1,string s2){studentid = s1;studentname = s2;}public DateTime BirthDate{get { return birthdate; }set { birthdate = value; }}public string BirthPlace{get { return birthplace; }set { birthplace = value; }}public string StudentName{get { return studentname; }}public string StudentId{get { return studentid; }}public string Age{get{DateTime now = new DateTime();now = DateTime.Parse("2021-12-10");TimeSpan ts = now - birthdate;int age = ts.Days / 365;return age.ToString();}}
?