123

C# 抽象类

 

 1 namespace ConsoleApplication2
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7            Football footBall =new Football();
 8            BasketBall basketBall=new BasketBall();
 9            //调用PlayBall方法
10            PlayBall(footBall);
11            PlayBall(basketBall);
12             Console.ReadKey();
13         }
14 
15         static void PlayBall(Ball ball)
16         {
17             Console.WriteLine("球类:{0}",ball.Ballname);
18             ball.Play();
19         }
20     }
21 
22     public abstract class Ball
23     {
24         /// <summary>
25         /// 球的名字
26         /// </summary>
27         public abstract string Ballname { get; }
28 
29         /// <summary>
30         /// 打球
31         /// </summary>
32         public abstract void Play();
33     }
34 
35     public class Football :Ball
36     {
37         public override string Ballname
38         {
39             get { return "足球"; }
40         }
41 
42         public override void Play()
43         {
44             Console.WriteLine("正在踢足球...");
45         }
46     }
47     public class BasketBall : Ball
48     {
49         public override string Ballname
50         {
51             get { return "篮球"; }
52         }
53 
54         public override void Play()
55         {
56             Console.WriteLine("正在打篮球...");
57         }
58     }
59 }

 

posted @ 2017-08-25 11:16  thy22  阅读(171)  评论(0编辑  收藏  举报