C#设计这个类people 和它的两个派生类student employee 并进行测试
1个回答
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyApplication
{
public class People
{
public People(string name, int age)
{
Name = name;
Age = age;
}
public string Name { get; set; }
public int Age { get; set; }
public virtual void Display()
{
Console.WriteLine("我的名字是{0},今年{1}岁", Name, Age);
}
}
public class Student : People
{
public Student(string name, int age, string school)
: base(name, age)
{
School = school;
}
public string School { get; set; }
public override void Display()
{
Console.WriteLine("我的名字是{0},今年{1}岁,正在{2}学校读书",
Name, Age, School);
}
}
public class Employee : People
{
public Employee(string name, int age, string company)
: base(name, age)
{
Company = company;
}
public string Company { get; set; }
public override void Display()
{
Console.WriteLine("我的名字是{0},今年{1}岁,正在{2}公司上班",
Name, Age, Company);
}
}
// 测试程序
class Program
{
static void Main(string[] args)
{
Student s = new Student("李宏", 17, "行知中学");
Employee e = new Employee("张平", 25, "中兴通讯");
People p;
p = s;
p.Display();
p = e;
p.Display();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询