首页 > 代码库 > abstract

abstract

 1 /// <summary>
 2     /// 抽象类不能被直接实例化
 3     /// is a
 4     /// </summary>
 5     public abstract class BasePhone
 6     {
 7         public void Show()
 8         {
 9             Console.WriteLine("");
10         }
11 
12         public int Price { get; set; }
13 
14         /// <summary>
15         /// 品牌
16         /// </summary>
17         /// <returns></returns>
18         public abstract string Brand();
19 
20         /// <summary>
21         /// 系统
22         /// </summary>
23         /// <returns></returns>
24         public abstract string System();
25 
26         /// <summary>
27         /// 打电话
28         /// </summary>
29         public abstract void Call();
30 
31         /// <summary>
32         /// 拍照
33         /// </summary>
34         public abstract void Photo();

 

2.基本接口作为约束

技术分享
 1     /// <summary>
 2     /// can do
 3     /// </summary>
 4     public interface IExtend
 5     {
 6         void ApplePay();
 7     }
 8 
 9     public interface INet
10     {
11         void Online();
12     }
接口

3.各个手机的实现

技术分享
 1     /// <summary>
 2     /// 手机
 3     /// </summary>
 4     public class iPhone : BasePhone, IExtend, INet
 5     {
 6         /// <summary>
 7         /// 品牌
 8         /// </summary>
 9         /// <returns></returns>
10         public override string Brand()
11         {
12             return "iPhone";
13         }
14 
15         /// <summary>
16         /// 系统
17         /// </summary>
18         /// <returns></returns>
19         public override string System()
20         {
21             return "IOS";
22         }
23 
24         /// <summary>
25         /// 打电话
26         /// </summary>
27         public override void Call()
28         {
29             Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
30         }
31 
32         /// <summary>
33         /// 拍照
34         /// </summary>
35         public override void Photo()
36         {
37             Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
38         }
39 
40         public void ApplePay()
41         {
42             Console.WriteLine("User{0} {1} {2} ApplePay", this.GetType().Name, this.Brand(), this.System());
43         }
44 
45         public void Online()
46         {
47             Console.WriteLine("User{0} {1} {2} Online", this.GetType().Name, this.Brand(), this.System());
48         }
49     }
iPhone
技术分享
 1     /// <summary>
 2     /// 手机
 3     /// </summary>
 4     public class Lumia : BasePhone
 5     {
 6         /// <summary>
 7         /// 品牌
 8         /// </summary>
 9         /// <returns></returns>
10         public override string Brand()
11         {
12             return "Nokia";
13         }
14 
15         /// <summary>
16         /// 系统
17         /// </summary>
18         /// <returns></returns>
19         public override string System()
20         {
21             return "Winphone";
22         }
23 
24         /// <summary>
25         /// 打电话
26         /// </summary>
27         public override void Call()
28         {
29             Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
30         }
31 
32         /// <summary>
33         /// 拍照
34         /// </summary>
35         public override void Photo()
36         {
37             Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
38         }
39     }
Lumia
技术分享
 1     public class Mi : BasePhone
 2     {
 3         /// <summary>
 4         /// 品牌
 5         /// </summary>
 6         /// <returns></returns>
 7         public override string Brand()
 8         {
 9             return "XiaoMi";
10         }
11 
12         /// <summary>
13         /// 系统
14         /// </summary>
15         /// <returns></returns>
16         public override string System()
17         {
18             return "Android";
19         }
20 
21         /// <summary>
22         /// 打电话
23         /// </summary>
24         public override void Call()
25         {
26             Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
27         }
28 
29         /// <summary>
30         /// 拍照
31         /// </summary>
32         public override void Photo()
33         {
34             Console.WriteLine("User{0} {1} {2} Photo", this.GetType().Name, this.Brand(), this.System());
35         }
36     }
Mi

abstract用override写,接口直接实现.

 

abstract