关于C#编程问题,望解答,谢谢,悬赏80分!!
C#程序:
using System;
class Program
{
static void Main()
{
clsStudent s = new clsStudent("张三其", "201207330001", "男", 86);
s.Display();
clsTeacher t = new clsTeacher("李师", "A1231", "女", 10);
t.Name = "李师煊";
t.Display();
}
}
//人员类
class clsPerson
{
protected String name; //姓名
protected String id; //编号
protected String gender; //性别
//构造方法
public clsPerson()
{
name = null;
id = null;
gender = null;
}
//构造方法
public clsPerson(String name, String id, String gender)
{
this.name = name;
this.id = id;
this.gender = gender;
}
//“姓名”属性
public String Name
{
get
{
return name;
}
set
{
name = value;
}
}
//“编号”属性
public String Id
{
get
{
return id;
}
set
{
id = value;
}
}
//“性别”属性
public String Gender
{
get
{
return gender;
}
set
{
gender = value;
}
}
}
//学生类
class clsStudent : clsPerson
{
protected double score; //成绩
//构造方法
public clsStudent():base()
{
score = 0;
}
//构造方法
public clsStudent(String name, String id, String gender, double score)
: base(name, id, gender)
{
this.score = score;
}
public void Display()
{
Console.WriteLine("学生信息:");
Console.WriteLine("姓名:{0}", Name);
Console.WriteLine("编号:{0}", id);
Console.WriteLine("性别:{0}", gender);
Console.WriteLine("成绩:{0}", score);
}
}
//教师类
class clsTeacher : clsPerson
{
protected double teachAge; //教龄
//构造方法
public clsTeacher()
: base()
{
teachAge = 0;
}
//构造方法
public clsTeacher(String name, String id, String gender, double teachAge)
: base(name, id, gender)
{
this.teachAge = teachAge;
}
public void Display()
{
Console.WriteLine("教师信息:");
Console.WriteLine("姓名:{0}", name);
Console.WriteLine("编号:{0}", id);
Console.WriteLine("性别:{0}", gender);
Console.WriteLine("教龄:{0}", teachAge);
}
}
运行测试:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class Person
{
//编号
private string number;
public string Number { get; set; }
//姓名
private string name;
public string Name { get; set; }
//性别
private string sex;
public string Sex { get; set; }
public Person(string number, string name, string sex)
{
this.number = number;
this.name = name;
this.sex = sex;
}
public virtual void Show()
{
System.Console.Write(string.Format("编号:{0} , 姓名:{1} , 性别:{2} ", this.number, this.name, this.sex));
}
}
public class Student : Person
{
//成绩
private int score;
public int Score
{
get { return score; }
set { score = value; }
}
public Student(string number, string name, string sex)
: base(number, name, sex) { }
public override void Show()
{
base.Show();
System.Console.Write(string.Format("成绩:{0} ", this.score));
}
}
public class Teacher : Person
{
//教龄
private int experience;
public int Experience
{
get { return experience; }
set { experience = value; }
}
public Teacher(string number, string name, string sex)
: base(number, name, sex) { }
public override void Show()
{
base.Show();
System.Console.Write(string.Format("教龄:{0} ", this.experience));
}
}
}
调用的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Person person = new Person("001", "xiaoli", "man");
person.Show();
Student student = new Student("002", "xiaowamg", "man");
student.Score = 80;
student.Show();
Teacher teacher = new Teacher("002", "xiaowamg", "man");
teacher.Experience = 20;
teacher.Show();
System.Console.ReadKey();
}
}
}
{
static void Main(string[] args)
{
clsStudent s = new clsStudent();
s.name = "小小生";
s.成绩 = "100";
s.say();
clsTeacher t = new clsTeacher();
t.name = "123";
t.教龄 = "20";
t.say();
Console.ReadKey();
}
}
abstract class clsPerson
{
public string name{set;get;} //定义公共属性name,sex,age
public string sex { set; get; }
public string age { set; get; }
public abstract void say(); //定义抽象的say方法,子类里边必须包含此方法
}
class clsStudent : clsPerson //学生类继承porson类
{
public string 成绩 { get; set; }
public override void say() //只需要实现say方法就可以了,属性字段不用重新定义了
{
Console.WriteLine("我的名字是:{0},我的成绩是{1}",this.name,this.成绩);
}
}
class clsTeacher : clsPerson
{
public string 教龄 { get; set; }
public override void say()
{
Console.WriteLine("我的名字是:{0},我的教龄是{1}", this.name,this.教龄);
}
}
}
我这里用的抽象类,有不明白的可以问我e_mail:hackerxxs@163.com
嗯,好,谢谢
不好意思,电脑拿去修了。。。