- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
IList<T>
不继承 IList
其中 IEnumerable<out T>
继承IEnumerable
.
如果out
修饰符是大多数执行 IList<T>
的唯一原因(例如 Collection<T>
, List<T>
)实现 IList
界面。
所以任何人都可以说 OK,如果该陈述对于 IList<T>
的所有实现都是正确的然后直接投给IList
必要时。但问题是虽然 IList<T>
不继承IList
所以不能保证每个IList<T>
对象是 IList
.
此外使用 IList<object>
显然不是解决方案,因为没有 out
不能将修饰泛型分配给较少继承的类;并且创建新的 List 实例不是这里的解决方案,因为有人可能想要实际引用 IList<T>
作为IList
指针;并使用 List<T>
代替IList<T>
实际上是一种糟糕的编程习惯,并不能达到所有目的。
如果 .NET 想要为 IList<T>
的每个实现提供灵 active 不应该有非泛型实现的契约(即 IList
)那么为什么他们不保留另一个实现泛型和非泛型版本的接口(interface)并且不建议所有想要为泛型和非泛型契约的具体类非基因元素应通过该接口(interface)收缩。
类型转换时出现同样的问题ICollection<T>
至 ICollection
和 IDictionary<TKey, TValue>
至 IDictionary
.
最佳答案
如您所见,T
在IList<T>
不是协变的。根据经验:任何可以修改其状态的类都不能是协变的。原因是此类类通常具有具有 T
的方法。作为其参数之一的类型,例如void Add(T element)
.并且输入位置不允许协变类型参数。
除其他原因外,添加泛型是为了提供类型安全。例如,您不能添加 Elephant
到 Apple
的列表.如果ICollection<T>
将延长 ICollection
, 然后你可以打电话 ((ICollection)myApples).Add(someElephant)
没有编译时错误,如 ICollection
有一个方法 void Add(object obj)
,这似乎允许您将任何对象添加到列表中,而实际上您只能添加 T
的对象。 .因此,ICollection<T>
不扩展 ICollection
和 IList<T>
不扩展 IList
.
Anders Hejlsberg,C# 的创建者之一,explains it like this :
Ideally all of the generic collection interfaces (e.g.
ICollection<T>
,IList<T>
) would inherit from their non-generic counterparts such that generic interface instances could be used both with generic and non-generic code.As it turns out, the only generic interface for which this is possible is
IEnumerable<T>
, because onlyIEnumerable<T>
is contra-variant [sic1]: InIEnumerable<T>
, the type parameterT
is used only in "output" positions (return values) and not in "input" positions (parameters).ICollection<T>
andIList<T>
useT
in both input and output positions, and those interfaces are therefore invariant.
1) IEnumerable<T>
是共-变体
从 .Net 4.5 开始有 IReadOnlyCollection<out T>
和 IReadOnlyList<out T>
协变接口(interface)。但是IList<T>
, ICollection<T>
许多列表和集合类没有实现或扩展它们。坦率地说,我发现它们不是很有用,因为它们只定义了 Count
。和 this[int index]
.
如果我可以从头开始重新设计 .Net 4.5,我会将列表接口(interface)拆分为只读协变接口(interface) IList<out T>
其中包括 Contains
和 IndexOf
, 和一个可变的不变接口(interface) IMutableList<T>
.然后你可以投 IList<Apple>
至 IList<object>
.我在这里实现了这个:
M42 Collections - Covariant collections, lists and arrays.
关于c# - 为什么泛型 IList<> 不继承非泛型 IList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14559070/
我使用的是 PHP 5.3 稳定版,有时会遇到非常不一致的行为。据我所知,在继承中,父类(super class)中的所有属性和方法(私有(private)、公共(public)和 protected
所以我一直在努力寻找正确的方法来让应该非常简单的继承发挥作用(以我想要的方式 ;)),但我失败得很惨。考虑一下: class Parent { public String name = "Pare
给定这些类: class Father { public Father getMe() { return this; } } class Child extends Father {
为什么最后打印“I'm a Child Class”。 ? public class Parent { String parentString; public Parent()
我知道有很多类似的问题对此有很多很好的答案。我试着看看经典的继承方法,或者那些闭包方法等。不知何故,我认为它们对我来说或多或少是“hack”方法,因为它并不是 javascript 设计的真正目的。
我已经使用表单继承有一段时间了,但没有对以下方法进行太多研究。只需创建一个新类而不是表单并从现有表单继承并根据需要将所需控件转换为 protected 。 Visual Studio 2010 设计器
我原以为下面的代码片段会产生编译错误,因为派生类不会有我试图在 pub_fun() 中访问的 priv_var。但是它编译了,我得到了下面提到的输出。有人可以解释这背后的理论吗? class base
继承的替代方案有哪些? 最佳答案 Effective Java:优先考虑组合而不是继承。 (这实际上也来自《四人帮》)。 他提出的情况是,如果扩展类没有明确设计为继承,继承可能会导致许多不恰当的副作用
我有2个类别:动物( parent )和狗(动物的“ child ”),当我创建一个 Animal 对象并尝试提醒该动物的名称时,我得到了 undefined ,而不是她的真名。为什么?(抱歉重复发帖
我试图做继承,但没想到this.array会像静态成员一样。我怎样才能让它成为“ protected /公开的”: function A() { this.array = []; } func
在创建在父类中使用的 lambda 时,我试图访问子类方法和字段。代码更容易解释: class Parent { List> processors; private void do
如果我有一个对象,我想从“ super 对象”“继承”方法以确保一致性。它们将是混合变量。 修订 ParentObj = function() { var self = this; t
class Base { int x=1; void show() { System.out.println(x); } } class Chi
目前我正在尝试几种不同的 Javascript 继承方法。我有以下代码: (“借用”自 http://www.kevlindev.com/tutorials/javascript/inheritanc
我在 .popin-foto 元素中打开一个 popin。当我尝试在同一元素中打开子类 popin 时,它不起作用。 代码 这是 parent function Popin(container, ti
我有以下两个类: class MyClass { friend ostream& operatorvalue +=1; return *this; } 现在
有没有办法完全忽略导入到 html 文件中的 header 中的 CSS 文件? 我希望一个页面拥有自己独立的 CSS,而不是从任何其他 CSS 源继承。 最佳答案 您可以在本地样式表中使用 !imp
Douglas Crockford似乎喜欢下面的继承方式: if (typeof Object.create !== 'function') { Object.create = functio
假设我有以下代码: interface ISomeInterface { void DoSomething(); void A(); void B(); } public
class LinkedList{ public: int data; LinkedList *next; }; class NewLinkedList: public Lin
我是一名优秀的程序员,十分优秀!