定义一个人类,
包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉,工作。
(1)根据人类,派生一个学生类,增加属性:学校、学号;重写工作方法(学生的工作是学习)。
(2)根据人类,派生一个工人类,增加属性:单位、工龄;重写工作方法(工人的工作是……自己想吧)。
(3)根据学生类,派生一个学生干部类,增加属性:职务;增加方法:开会。
(4)编写主函数分别对上述
CLass1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
public class Class1
{
public static void Main ()
{
OneStudent onestudent = new OneStudent("marry", 18, "woman", "TYUT", 222);
onestudent.eat();
onestudent.sleep();
onestudent.work();
OneStudentS oness = new OneStudentS("Jack", 18, "man", "学生会主席", "TYUT", 333);
oness.meeting();
OneWorker oneworker = new OneWorker("Tom", 22, "man", "ChinaSoft", 2);
oneworker.eat();
oneworker.sleep();
oneworker.work();
Console.ReadLine();
}
}
}
One.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
public class One
{
public string name;
public string sex;
public int age;
public One(string n, string s, int a)
{
this.name = n;
this.sex = s;
this.age = a;
}
public virtual void eat()
{
Console.WriteLine("{0} is eating~",name);
}
public virtual void sleep()
{
Console.WriteLine("{0} is sleeping~", name);
}
public virtual void work()
{
Console.WriteLine("{0}的工作是不知道",name);
}
}
}
OneStudent.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
public class OneStudent:One
{
public string school;
public int sid;
public OneStudent(string n,int a,string s,string school,int sid):base(n,s,a)
{
//name=n;
//age=a;
//sex=s;
this.school=school;
this.sid=sid;
}
public override void work()
{
// base.work();
Console.WriteLine("姓名:{0},性别:{1},学校:{2},学号:{3},年龄:{4}", name, sex, school, sid, age);
Console.WriteLine("{0}的工作是学习!", name);
}
//public void work(string school, int sid, string n, int a, string s)
//{
//}
}
}
OneWorker.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class OneWorker:One
{
private string place;
private int workage;
public OneWorker(string n,int a,string s,string p,int w):base(n,s,a)
{
place = p;
workage = w;
}
public override void work()
{
Console.WriteLine("姓名:{0},年龄:{1},性别:{2},工作单位:{3},工龄:{4}",name,age,sex,place,workage);
Console.WriteLine("{0}的工作是教学。", name);
}
}
}
OneStudentS.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class OneStudentS:OneStudent
{
private string job;
public OneStudentS(string n, int a, string s, string job,string school,int sid)
: base(n, a, s, school, sid)
{
this.job = job;
}
public void meeting()
{
Console.WriteLine("姓名:{0},年龄:{1},性别:{2},职务:{3},学校:{4},学号:{5}", name, age, sex, job, school, sid);
Console.WriteLine("他正在开会~");
}
}
}
父-子-孙 在一起
最新推荐文章于 2024-08-18 19:33:19 发布