c#继承方法的使用

定义一个学生类(Student),有stuName私有字段及相应属性,有Disp方法,显示学生姓名;
定义大学生类(CollegeStudent)继承基类Student,有Specialty公有字段和Practice方法,输出学生参与的项目名称;
在主函数中实例化一个CollegeStudent的对象,并分别调用基类的方法和自身类的方法。

//
class Student
    {
    
    
        string stuName;

        public string StuName
        {
    
    
            get {
    
     return stuName; }
            set {
    
     stuName = value; }
        }
        public void disp()
        {
    
    
            Console.WriteLine("{0},就是我,我就是{0}!", this.stuName);
        }
    }
 class CollegeStudent : Student
    {
    
    
        public string specialty;
        public void practice()
        {
    
    
            Console.WriteLine("我参与了学校的{0}项目", this.specialty);
        }
    }
 CollegeStudent cs1 = new CollegeStudent();//给类定义一个对象
            cs1.StuName = "屠呦呦";
            cs1.specialty = "生物工程";
            cs1.disp();
            cs1.practice();

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41920585/article/details/109789698