本帖最后由 吴承烨 于 2013-6-18 20:55 编辑
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 里氏转换数组
- {
- class Program
- {
- static void Main(string[] args)
- {
- //用数组弄2个人出来
- Sun sun = new Sun("吴沉烨", "女", "女人", 100);
- Sun1 sun1 = new Sun1("小明", "男", "男人", 180);
- Preson[] preson = { sun, sun1 };
- for (int i = 0; i < preson.Length; ++i)
- {
- //循环里头怎么打印出Say方法
- }
- //((Sun)preson[0]).Say();
- //((Sun1)preson[1]).Say();
- Console.ReadKey();
- }
- }
- //父类
- class Preson
- {
- public Preson(string name, string gender, string hobby)
- {
- this.name = name;
- this.gender = gender;
- this.hobby = hobby;
- }
- //姓名
- string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- //性别
- string gender;
- public string Gender
- {
- get { return gender; }
- set { gender = value; }
- }
- //爱好
- private string hobby;
- public string Hobby
- {
- get { return hobby; }
- set { hobby = value; }
- }
- //方法
- public void SayHello()
- {
- Console.WriteLine("我叫{0},性别{1},爱好{2}", name, gender, hobby);
- }
- }
- //子类1
- class Sun : Preson
- {
- //构造方法
- public Sun(string name, string genter, string hobby, int fenshu)
- : base(name, genter, hobby)
- {
- this.fenshu = fenshu;
- }
- //分数
- int fenshu;
- public int Fenshu
- {
- get { return fenshu; }
- set { fenshu = value; }
- }
- public void Say()
- {
- Console.WriteLine("我的名字叫{0},性别{1},爱好{2},分数{3}",Name,Gender,Hobby,fenshu);
- }
- }
- //子类2
- class Sun1 : Preson
- {
- //构造方法
- public Sun1(string name, string genter, string hobby,int shengao)
- : base(name, genter, hobby)
- {
- this.shengao = shengao;
- }
- //身高
- int shengao;
- public int Shengao
- {
- get { return shengao; }
- set { shengao = value; }
- }
- public void Say()
- {
- Console.WriteLine("我的名字叫{0},性别{1},爱好{2},身高{3}", Name, Gender, Hobby, Shengao);
- }
- }
- }
复制代码 |