// 练习:
// 写一个Person和Student,姓名(name)性别(gender)年龄(age)由父类构造方法初始化
// 年龄默认为0,性别随机,姓名有子类指定调用
// 创建一个Student对象,并做自我介绍
// 同时实现:实例化一个Student对象,指定姓名,性别,年龄

namespace 继承_封装_多态
{

class Program
{
static void Main(string[] args)
{

Student s1 = new Student("张三",12);
s1.SayHello();
Student s2 = new Student("小王", '男', 25, 430);
s2.SayHello();
Console.ReadKey();
}
class Person
{
string name;

public string Name
{
get { return name; }
set { name = value; }
}
char gender;

public char Gender
{
get { return gender; }
set { gender = value; }
}
int age;

public int Age
{
get { return age; }
set { age = value; }
}
public Person(string name)
{
this.name = name;
Random r = new Random();
gender = r.Next() % 2 == 0 ? '男' : '女';
age = 0;
}
public Person(string name, char gender, int age)
{
this.name = name;
this.gender = gender;
this.age = age;
}
public Person()
{ }
}
class Student:Person
{
int chengji;

public int Chengji
{
get { return chengji; }
set { chengji = value; }
}
public void SayHello()
{
Console.WriteLine("{0}{1}{2}{3}.",Name,Gender,Age,chengji);
}
public Student(string name, char gender, int age, int chengji)
: base(name, gender, age)
{
this.chengji = chengji;
}
public Student(string name, int chengji)
: base(name)
{
this.chengji = chengji;
}

}

}

posted on 2012-07-13 00:14  Fan帥帥  阅读(139)  评论(0编辑  收藏  举报