本帖最后由 czwanglei 于 2014-5-11 17:28 编辑
- Person类
- class Person
- {
- private string name; //定义人的名字
- private int age; //人的年龄
- private char gender; //人的性别
- public string Name //下面是分别对三个字段的封装
- {
- get { return name; }
- set { name = value; }
- }
- public int Age
- {
- get { return age; }
- set { age = value; }
- }
- public char Gender
- {
- get { return gender; }
- set { gender = value; }
- }
- public Person ()//显示定义一个构造方法,避免继承的时候出现调用无参的方法时报错
- { }
- public Person(string name,int age,char gender)//有参构造方法,为了给参数赋值
- {
- this.name = name;
- this.age = age;
- this.gender = gender;
- }
- /// <summary>
- /// 定义一个人打招呼的方法
- /// </summary>
- public new void SayHello()
- {
- Console.WriteLine("我是基类打招的方法");
- Console.WriteLine("大家好,我叫{0},今年{1}岁,我是{2}生.", name, age, gender);
- }
复制代码- Teacher类
- class Teacher:Person
- {
- private string name;
- private int age;
- private char gender;
- private int yearsofService;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- public int Age
- {
- get { return age; }
- set { age = value; }
- }
- public char Gender
- {
- get { return gender; }
- set { gender = value; }
- }
- public int YearsofService
- {
- get { return yearsofService; }
- set { yearsofService = value; }
- }
- /// <summary>
- /// Teacher的有参构造方法
- /// </summary>
- /// <param name="name">老师姓名</param>
- /// <param name="age">老师年龄</param>
- /// <param name="gender">老师性别</param>
- /// <param name="yearsofService"></param>
- public Teacher (string name,int age,char gender,int yearsofService):base(name,age,gender)
- {
- this.yearsofService = yearsofService;
- }
- /// <summary>
- /// 定义一个老师的说话的方法
- /// </summary>
- public void SayHello()
- {
- Console.WriteLine("我是父类打招呼的方法");
- Console.WriteLine("大家好,我叫{0},今年{1},教书{2}年了.",name,age,yearsofService);
- }
复制代码- 主函数
- class Program
- {
- static void Main(string[] args)
- {
- Teacher t = new Teacher("张老师", 29, '男', 3);
- t.SayHello();
-
- Console.ReadKey();
- }
复制代码 请问 t.SayHello(), 怎么没有获取到"张老师", 29, '男', 3的值。 |
|