gpt4 book ai didi

c# - C# 中的接口(interface)

转载 作者:太空狗 更新时间:2023-10-29 18:01:01 24 4
gpt4 key购买 nike

我是 OOP 的新手,有一些问题。

  1. 为什么在接口(interface)中声明的方法不能有修饰符(public、private 等)。

  2. 在这段代码中:

class Program
{
static void Main(string[] args)
{
X ob = new Y();
ob.add(4, 5);
Z ob1 = new Y();
ob1.mull(2, 3);
Console.Read();
}
}

public interface X
{
void add(int x, int y);
}
public interface Z
{
void mull(int x, int y);
}

class Y : X, Z
{
void X.add(int x, int y)//here we are not decalring it as public ,why?
{
Console.WriteLine("sum of X and y is " + (x + y));
}
void Z.mull(int x, int y)
{
Console.WriteLine("product of X and Y is" + (x * y));
}
}

这两种方法都不需要修饰符,但是当我不使用接口(interface)时,如上面的 X.add(),我需要将实现公开。为什么?

最佳答案

一个接口(interface)就是一个契约。它说“我可以做这些事情”。有人给你一个 IInterface 的实例有什么意义?其中您不能使用该契约(Contract)中的某些方法,因为它们已被标记为不公开?

这就是以这种方式设计语言的基本原理。对此的规范在语言规范的 §13.2 中:

All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, interfaces members cannot be declared with the modifiers abstract, public, protected, internal, private, virtual, override, or static.

至于您的代码,这是 explicit interface implementation 的示例.当一个类或结构实现两个接口(interface),每个接口(interface)都有一个具有相同签名的成员时,它最有用。例如,IEnumerableIEnumerable<T>定义一个方法 GetEnumerator不接受任何参数。

public interface IEnumerable {
IEnumerator GetEnumerator();
}

public interface IEnumerable<T> : IEnumerable {
IEnumerator<T> GetEnumerator();
}

请注意,根据上述定义,任何实现 IEnumerable<T> 的类还必须实现 IEnumerable .请记住,返回类型不是签名的一部分,因此我们与 IEnumerable.GetEnumerator 有冲突。和 IEnumerable<T>.GetEnumerator .这就是显式接口(interface)实现要解决的问题:

class X<T> : IEnumerable<T> {
List<T> _list = new List<T>();
public IEnumerator<T> GetEnumerator() {
return _list.GetEnumerator();
}

IEnumerator GetEnumerator() {
return GetEnumerator(); // invokes IEnumerable<T>.GetEnumerator
}
}

作为显式接口(interface)实现的成员仅通过接口(interface)实例可见。因此:

X<int> x = new X<int>();
var e1 = x.GetEnumerator(); // invokes IEnumerable<int>.GetEnumerator
// IEnumerable.GetEnumerator is not visible
IEnumerable y = x;
var e2 = y.GetEnumerator(); // invokes IEnumerable.GetEnumerator

因此,在您的代码中

X ob = new Y();
ob.add(1, 2); // X.add is visible through interface
Y y = new Y();
y.add(1, 2); // compile-time error, X.add is not visible

Both methods don't require modifiers but when I don't use interface, like X.add() above, I need to make the implementation public. Why?

好吧,目前还不清楚你在这里问的到底是什么。显式接口(interface)实现不允许使用访问修饰符。这是 13.4:

It is a compile-time error for an explicit interface member implementation to include access modifiers, and it is a compile-time error to include the modifiers abstract, virtual, override, or static.

如果接口(interface)实现未标记为显式接口(interface)实现,则它必须具有访问修饰符 public .这是 13.4.4(接口(interface)映射):

Interface mapping for a class or struct C locates an implementation for each member of each interface specified in the base class list of C. The implementation of a particular interface member I.M, where I is the interface in which the member M is declared, is determined by examining each class or struct S, starting with C and repeating for each successive base class of C, until a match is located

  • If S contains a declaration of an explicit interface member implementation that matches I and M, then this member is the implementation of I.M

  • Otherwise, if S contains a declaration of a non-static public member that matches M, then this member is the implementation of I.M.

A compile-time error occurs if implementations cannot be located for all members of all interfaces specified in the base class list of C.

所以,简而言之,编译器首先寻找显式接口(interface)实现。如果找不到,则它会寻找与方法具有相同签名的非静态、公共(public)成员M。正在实现。如果找不到,则会发生编译时错误。所以规则是这样的。实现一个接口(interface)成员I.M :

  1. 如果您实现 I.M明确地那么语法是

    return-type I.M(parameter-list)

  2. 否则语法为

    public return-type M(parameter-list)

因此,与

interface IAdd {
int Add(int x, int y)
}

我们可以显式实现:

class Explicit : IAdd {
int IAdd.Add(int x, int y) { return x + y; }
}

或不:

class NotExplicit : IAdd {
public int Add(int x, int y) { return x + y; }
}

区别在于Explicit.Add不可见,除非 Explicit 的实例类型为 IAdd :

IAdd explicitInterface = new Explicit();
explicitInterface.Add(2, 2);
Explicit explicit = new Explicit();
explicit.Add(2, 2); // compile-time error

鉴于

IAdd notExplicitInterface = new NotExplicit();
notExplicitInterface.Add(2, 2);
NotExplicit notExplicit = new NotExplicit();
notExplicit.Add(2, 2); // okay, NOT a compile-time error as above

这有帮助吗?

关于c# - C# 中的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2164451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com