如何在C#中向接口添加成员变量

发布于 2024-08-28 01:52:10 字数 443 浏览 4 评论 0原文

我知道这可能是基本的,但我似乎无法向接口添加成员变量。 我尝试将接口继承到抽象类并向抽象类添加成员变量,但它仍然不起作用。这是我的代码:

public interface IBase {
  void AddData();
  void DeleteData();
}

public abstract class AbstractBase : IBase {
  string ErrorMessage;
  public abstract void AddData();
  public abstract void DeleteData();
}

public class AClass : AbstractBase {
  public override void AddData();
  public override void DeleteData();
}

根据罗伯特·弗雷泽的评论更新

I know this may be basic but I cannot seem to add a member variable to an interface.
I tried inheriting the interface to an abstract class and add member variable to the abstract class but it still does not work. Here is my code:

public interface IBase {
  void AddData();
  void DeleteData();
}

public abstract class AbstractBase : IBase {
  string ErrorMessage;
  public abstract void AddData();
  public abstract void DeleteData();
}

public class AClass : AbstractBase {
  public override void AddData();
  public override void DeleteData();
}

updated base on comment of Robert Fraser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

年少掌心 2024-09-04 01:52:10

您不能向接口添加字段。接口只能包含方法,因此只能在接口声明中声明方法、属性、事件。您可以使用属性来代替字段。

public interface IBase {  
  string ErrorMessage {get;set;}
  void AddData();  
  void DeleteData();  
}

You cannot add fields to an interface.Interface can only contain methods , so only methods , properties , events can be declared inside an interface decleration.In place of field you can use a property.

public interface IBase {  
  string ErrorMessage {get;set;}
  void AddData();  
  void DeleteData();  
}
雪若未夕 2024-09-04 01:52:10
public interface IBase {
  void AddData();
  void DeleteData();
}

public abstract class AbstractBase : IBase {
  string ErrorMessage;
  public abstract void AddData();
  public abstract void DeleteData();
}

为我工作。您缺少抽象类方法上的“public”和“void”。

public interface IBase {
  void AddData();
  void DeleteData();
}

public abstract class AbstractBase : IBase {
  string ErrorMessage;
  public abstract void AddData();
  public abstract void DeleteData();
}

Workd for me. You were missing the "public" and "void" on the abstract class methods.

情深如许 2024-09-04 01:52:10

由于接口仅声明非实现细节,因此您不能在接口中声明成员变量。

但是,您可以在接口上定义属性,并在类中实现该属性。

Since interfaces only declare non-implementation details, you cannot declare a member variable in an interface.

However, you can define a property on your interface, and implement the property in your class.

北城孤痞 2024-09-04 01:52:10

如果它是实现类需要实现的内容,则使用属性定义 ie

也就是说,如果它是需要私有的东西,那么它无论如何都不应该出现在界面中。

If it is something implementing classes need to implement then use a property definition i.e.

That said if it is something which needs to be private then that it not something which should be in an interface anyway.

撩起发的微风 2024-09-04 01:52:10

您指的是属性而不是字段吗?

public interface IBase {
  string ErrorMessage { get; set; }
  void AddData();
  void DeleteData();
}

public abstract class AbstractBase : IBase {
  abstract string ErrorMessage { get; set; }

  // also, you need to declare a return type for the methods
  abstract void AddData();
  abstract void DeleteData();
}

Did you mean a property instead of a field?

public interface IBase {
  string ErrorMessage { get; set; }
  void AddData();
  void DeleteData();
}

public abstract class AbstractBase : IBase {
  abstract string ErrorMessage { get; set; }

  // also, you need to declare a return type for the methods
  abstract void AddData();
  abstract void DeleteData();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文