c# - 如果一个类正在使用一个接口(interface),它必须

标签 c# class inheritance interface

If a class is using an interface, it must:

  • (1) inherit the properties of the interface

  • (2) contain the same methods as the interface

  • (3) create an interface object

  • (4) all of the above

正确答案是 (4)以上所有

注意:这来自here而且我不知道这个问题的作者到底是谁。

我同意 (1)st 和 (2)nd,但我会争论 (3)rd 的措辞。

我猜它可以被双重解释;

创建一个接口(interface)对象可能只是一个继承接口(interface)的类的实例

接口(interface)类型的引用变量


正确的解读方式是什么?

以下是否有意义?

If a class is using an interface, it must:

  • (1) inherit the properties of the interface

  • (2) provide implementation for all methods that the interface exposes

  • (3) ???

  • (4) all of the above

最佳答案

(2) provide implementation for all methods that the interface exposes

没有。抽象类可以在不提供实现的情况下实现接口(interface)(它们仍然必须声明接口(interface)中定义的方法,但它们不必提供主体)。

不过我也不知道如何解释数字 3。通过您提供的链接查看时,这只是问题措辞不当的问题。我仍在尝试弄清楚这意味着什么:

  1. 代码 public class B : A { } 的输出是什么
    • 错误
    • 它定义了一个只继承A的公共(public)方法的类。
    • 定义了一个继承A所有方法但私有(private)成员不可访问的类。
    • b和c

关于c# - 如果一个类正在使用一个接口(interface),它必须,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218708/

相关文章:

c# - 以 xamarin 形式将 base64 字符串绑定(bind)到 ListView

c# - CookieContainer.SetCookies 只设置第一个

c# - 为什么在这种情况下需要绑定(bind)

c# - 带有自定义响应包装器的 ASP.NET Core 3.1 Web API 中的 JSON 响应中断

Python改变类函数

c++ - 从使用不完整类型实例化的模板继承

swift - 根据函数参数创建类的实例

c++ - 在 C++ 中创建另一个类中的类的对象

c# - 如何将 Animal 实例变成 Dog 实例?

java - 子对象可以与派生类的其余部分分开分配吗