编写一个类Student1,代表学员,要求:
(1)具有属性:姓名、年龄、性别、专业
(2)具有方法:自我介绍,负责输出该学员的姓名、年龄、性别、以及专业
(3)具有两个带参构造方法:第一个构造方法中,设置学员的性别为男、专业为计算机,其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定
(4)编写测试类
class Program
{
static void Main(string[] args)
{
Student1 Li = new Student1("李小明",18);
Student1 Sun = new Student1("孙亮",20,"男","金融");
Li.showStu();
Sun.showStu();
}
}
class Student1
{
private string name;
private int age;
private string sex;
private string major;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value;}
}
public string Sex
{
get { return sex; }
set { sex = value; }
}
public string Major
{
get { return major; }
set { major = value; }
}
public Student1(string name,int age)
{
this.name = name;
this.age = age;
this.sex = "男";
this.major = "计算机";
}
public Student1(string name, int age, string sex, string major)
{
this.name = name;
this.age = age;
this.sex = sex;
this.major = major;
}
public void showStu()
{
Console.WriteLine("姓名:{0}\t年龄:{1}\t性别:{2}\t专业:{3}", this.name,
this.age,this.sex,this.major);
}
}