(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分)
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);
}
}
}
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);
}
}
}