【发布时间】:2019-06-06 02:20:29
【问题描述】:
这与语言无关,因为我的问题适用于任何具有interface 概念的语言。 (或快速协议)
在 C# 中考虑这个程序:
interface Inter1
{
int Count();
}
interface Inter2
{
int Count();
}
public class SomeClass : Inter1, Inter2
{
public int Count() // which one is it ?
{
return 0;
}
}
static void Main(string[] args)
{
Inter1 c = new SomeClass();
int i = c.Count();
}
或者在 C++ 中: https://godbolt.org/z/dFLpji
我很难理解为什么这是可以容忍的,尽管看起来所有可能的符号引用都是明确的,因为静态类型将指定我们正在谈论的函数。
但是隐藏名字不是很危险吗?
我正在考虑的问题的说明:
// in FileA.cs on day 0
interface DatabaseCleaner
{
void Execute();
}
// in FileFarAway.cs day 140
interface FragmentationLogger
{
void Execute(); // unfortunate naming collision not noticed
}
// in PostGreAgent.cs day 141
public class PostGreAgent : DatabaseCleaner, FragmentationLogger
{
public void Execute()
{ // I forgot about the method for database cleanup, or dismissed it mentally for later. first let's test this
allocator.DumpFragmentationInfo();
}
private Allocator allocator;
}
// client.cs
AgentsManager agents;
void Main(string[] args)
{
var pa = new PostGreAgent();
agents.Add(pa);
// ... later in a different location
DatabaseCleaner cleaner = agents.FindBest();
cleaner.Execute(); // uh oh
}
【问题讨论】:
-
所以您正在寻找语言允许这种情况的原因?
-
@jaco0646 谢谢,是的,您的链接是专门针对 java 的同一个问题。现在我想知道 java 的标准委员会是否明确知道他们对 2 种冲突方法强制执行相同的实现,并将其接受为“meh”或“是的,这是所需的”,或者他们根本没有考虑过。
-
@Spotted 绝对
标签: oop interface language-agnostic multiple-inheritance name-collision