友情链接: 互动百科 CSDN.NET 百度音乐 和讯理财 世界杯吧 拉手网

利用多态的优点简化代码

using System;
using System.Collections.Generic;
using System.Text;
/*
 多态简化了代码的维护
 * 如果不是多态的时候
 */
namespace ConsoleApplication1
{
 public    class Student
    {
        private string name;
        private string studentId;
        private string major;
        private double gpa;
        public virtual void Print()
        {
            Console.WriteLine("student name"+name+"\n"+"sutent.no"+studentId+"\n"+"major field"+major+"\ngpa"+gpa);
        }
    }
    public class GraduateStudent : Student
    {
        private string unsergraducteaDegreee;
        private string undergraduacteinsittuion;
        public override void Print()
        {
            base.Print();
            Console.WriteLine("ddd");
        }
    }
    public class undergratestudent : Student
    {

        private string undergratestuet;
        public override void Print()
        {   
            base.Print();
            Console.WriteLine("ddd");
        }
    }
    class zhu
    {
        static void Main(string[] args)
        {
            Student[] studentbody = new Student[20];
            undergratestudent u1 = new undergratestudent();
            GraduateStudent u2 = new GraduateStudent();
            studentbody[0] = u1;
            studentbody[1] = u2;
            for (int i = 0; i < 20; i++)
            {

                studentbody[i].Print();
            }
        }

    }
}

posted on 2010-08-19 11:53  行万里路 责任 创新 执着  阅读(1261)  评论(3编辑  收藏  举报