C#程序設計練習題十二:簡單類及成員實例(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 s, string t)
        {
            studentid = s;
            studentname = t;
        }
        public DateTime BirthDate
        {
            get { return birthdate; }
            set { birthdate = value; }
        }
        public string BirthPlace
        {
            get { return birthplace; }
            set { birthplace = value; }
        }
        public string StudentName
        {
            get { return studentname; }
            set { studentname = value; }
        }
        public string StudentId
        {
            get { return studentid; }
            set { studentid = value; }
        }
        public int Age
        {
            get { return DateTime.Now.Year - birthdate.Year + 1; }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章