//关于多态
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();
   }
}
}
//这里对象会记住自己的标示不论送到任何基类引用它都会带着有色眼睛一样看自己的能力方法!!


相关文章:

  • 2022-02-08
  • 2022-12-23
  • 2022-01-01
  • 2021-08-03
  • 2022-12-23
  • 2021-11-24
  • 2021-09-04
  • 2021-07-02
猜你喜欢
  • 2021-06-03
  • 2022-02-14
  • 2022-01-04
  • 2021-11-21
  • 2021-12-28
  • 2022-01-13
  • 2021-04-22
相关资源
相似解决方案