继承中的构造函数
比如基类(人)姓名 性别 年龄
派生类(学生)班级 专业
要求使用构造函数
谢谢各位高手了
程序代码: class People
{
public People(string name, string gener, int age)
{
this.Name = name;
this.Gender = gener;
this.Age = age;
}
public string Name { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
}
class Student : People
{
public Student(string name, string gener, int age, string speciality, string classValue)
: base(name, gener, age)
{
this.Speciality = speciality;
this.Class = classValue;
}
public string Speciality { get; set; }
public string Class { get; set; }
}Student s = new Student("a", "b", 10, "c", "d");
程序代码:class Person
{
private string name ; //姓名
private char sex; //性别
private uint age; //年龄
public Person(){}
public Person(string name ,char sex , uint age)
{
Name = name;
Sex = sex;
Age = age;
}
public string Name
{
get{return name;}
set{this.name = value;}
}
public char Sex
{
get{return sex;}
set{this.sex = value;}
}
public uint Age
{
get{return age;}
set{this.age = value;}
}
}
class Student : Person
{
private string studentClass; //班级
private string specialty; //专业
public Student(){}
public Student(string studentClass , student specialty)
{
StudentClass = studentClass;
Specialty = specialty;
}
public Student(string name, char sex , uint age , string studentClass , string specialty) : base(name , sex , age)
{
StudentClass = studentClass;
Specialty = specialty;
}
public string StudentClass
{
get{return studentClass;}
set{this.studentClass = value;}
}
public string Specialty
{
get{return specialty;}
set{this.specialty = value;}
}
public String toString()
{
return "我叫" + this.Name + "。\n性别:" + this.Sex + "\n班级:" + this.StudentClass + "\n专业:" + Specialty;
}
}
class Test
{
Student ZS = new Student("张三" , '男' , 20 , "XX" , "计算机");
Console.WriteLine(ZS.toString());
}