利用多态的优点简化代码
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();
}
}
}
}