期末考试题

(1)根据下面的要求实现人类People
a)人类People的成员变量:
公共成员name表示姓名,为String类型(1分)
公共成员age  表示性别,为int类型(1分)
b)人类People的方法:
1)无参构造函数People ( ),将各成员变量初始化为“unknown”和20 。(1分)
2)构造函数People (string n, int a) (1分)
3)public virtual void disp( ) 将人的姓名、年龄输出到屏幕(1分)
(2)通过继承People类,派生出学生类Student 。要求如下:
a)学生类Student的成员变量:
公共成员department表示所在院系,为String类型(1分)
b)学生类Student的方法:
1)构造函数Student (string n, int a , string dep),用base 关键字调用父类中有两个参数的构造函数(2分)
2)重载覆盖父类中的函数 disp( ) 将学生的姓名、年龄、所在院系输出到屏幕(2分)

代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            people p = new people("张三", 18);
            p.disp();


            student s = new student("李四", 19, "计算机系");
            s.disp();
        }
    }


    class people
    {
        public string name;
        public int age;
        public people()
        {
            name = "unknown";
            age = 20;
        }
        /// <summary>
        /// 有参构造函数
        /// </summary>
        /// <param name="n">人的姓名</param>
        /// <param name="a">人的年龄</param>
        public people(string n,int a)
        {
            name = n;
            age = a;
        }
        public virtual void disp()
        {
            Console.WriteLine("姓名:{0},年龄:{1}",name,age);
        }
    }


    class student : people 
    {
        public string department;
        public student(string n,int a,string dep):base(n,a)
        {
            department = dep;
        }
        public void disp()
        {
            base.disp();
            Console.WriteLine("所在系部为:"+department);
        }
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值