C#学习小记10多态
//关于多态
using System;
public class Student
{
private string name;
public string Name
{
get {return name;}
set { name =value;}
}
public virtual void Print()
{
Console.WriteLine(Name);
}
}
public class StudentA:Student
{
private int age;
public int Age
{
get {return age;}
set {age=value;}
}
public override void Print()
{
base.Print();
Console.WriteLine(Age);
}
}
public class StudentB:Student
{
private bool boy;
public bool Boy
{
get {return boy;}
set {boy=value;}
}
public override void Print()
{
base.Print();
Console.WriteLine(Boy);
}
}
public class Do
{
static void Main()
{
Student[] studentBody=new Student[20];
StudentA u1=new StudentA();
StudentA u2=new StudentA();
StudentB g1=new StudentB();
StudentB g2=new StudentB();
studentBody[0]=u1;
studentBody[1]=g1;
studentBody[2]=g2;
studentBody[3]=u2;
for(int i=0;i<4;i++)
{
studentBody[i].Print();
}
}
}
//这里对象会记住自己的标示不论送到任何基类引用它都会带着有色眼睛一样看自己的能力方法!!