参考书目:C#6.0学习笔记——从第一行C#代码到第一个项目设计(作者周家安)P81
目的:抽象类的使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Example3_9
{
class Program
{
static void Main(string[] args)
{
FootBall football = new FootBall();
BasketBall basketball = new BasketBall();
//调用PlayBall方法
PlayBall(football);
PlayBall(basketball);
ReadKey();
}
//定义函数时输入参数为抽象类,使用时,可以传入各种派生类
static void PlayBall(Ball ball)
{
WriteLine("\n球类:{0}", ball.CateName);
}
}
//基类Ball,virual
public abstract class Ball
{
/// <summary>
/// 获取球类名称
/// </summary>
public abstract string CateName { get; }
public abstract void Play();
}
//派生类1:FootBall
public class FootBall :Ball
{
public override string CateName
{
get
{
return( "足球");
}
}
public override void Play()
{
WriteLine("正在踢足球");
}
}
//派生类2:BasketBall
public class BasketBall : Ball
{
public override string CateName
{
get
{
return ("篮球");
}
}
public override void Play()
{
WriteLine("正在打篮球");
}
}
}
运行结果: