本帖最后由 吴承烨 于 2013-6-18 20:53 编辑
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 里氏转换数组
- {
- class Program
- {
- static void Main(string[] args)
- {
- //用数组弄2个人出来
- Preson fa = new Preson("小明", "男");
- Sun sun = new Sun("吴沉烨", "女", "逗她");
- Preson [] preson ={fa, sun };
- for (int i = 0; i < preson.Length; ++i)
- {
- Console.WriteLine("{0} {1} {2}", preson[i].Name,preson[i].Gender,);//在这怎么遍历出子类方法?
- }
- Console.ReadKey();
- }
- }
- //父类
- class Preson
- {
- public Preson(string name, string gender)
- {
- this.name = name;
- this.gender = gender;
- }
- //姓名
- string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- //性别
- string gender;
- public string Gender
- {
- get { return gender; }
- set { gender = value; }
- }
- }
- //子类
- class Sun : Preson
- {
- //构造方法
- public Sun(string name, string genter, string hobby)
- : base(name, genter)
- {
- this.hobby = hobby;
- }
- //爱好
- string hobby;
- public string Hobby
- {
- get { return hobby; }
- set { hobby = value; }
- }
- //方法
- public void SayHello()
- {
- Console.WriteLine("我的名字叫{0},性别{1},爱好是{2}", Name, Gender, Hobby);
- }
- }
- }
复制代码 |