C# Interface 接口

来源:互联网 发布:淘宝开店免费教程 编辑:程序博客网 时间:2024/04/30 19:15

C# Interface 接口

接口就是在有若干个类,但这些类中有某些功能相同,但实现不同的方法,这个时候把这些方法写成接口,然后在每个类中分别加以实现。

例子

public class UserModel{    public static int Exp;}public interface IUserData{    int Exp    {        get;    }}public class Player : IUserData{    public int Exp    {        get { return 10+UserModel.Exp; }    }}public class VipPlayer : IUserData{    public int Exp    {        get { return 20+UserModel.Exp; }    }}    

普通玩家 Player 和 VIP 玩家 VipPlayer 的 Exp 计算是不一样的

0 0