class Program
{
List<Book> books = new List<Book>();
static void Main(string[] args)
{
Program p = new Program();
p.Test();
}
public void Test()
{
books.Add(new MathBook());
books.Add(new EnglishBook());
foreach (Book item in books)
{
//if (item is MathBook)
//{
// ((MathBook)item).reading();
//}
//if (item is EnglishBook)
//{
// ((EnglishBook)item).reading();
//}
item.reading();
}
}
}
class Book
{
public virtual void reading()
{
}
}
class MathBook:Book
{
//public void reading()
//{
// Console.WriteLine("我是数学书,被在校同学用");
//}
public override void reading()
{
Console.WriteLine("我是数学书,被在校同学用");
}
}
class EnglishBook:Book
{
//public void reading()
//{
// Console.WriteLine("我是英语书,被在校同学用");
//}
public override void reading()
{
Console.WriteLine("我是英语书,被在校同学用");
}
}
//
感谢前辈提供 谢谢啦!