假设我有: class Event {}
class DoorBell extends Event {}
class PhoneCall extends Event {}
class EventGenerator {
static Event getEvent() {
if (today.isSunday())
return new DoorBell()
我刚刚在我的编程语言课上学到“逆变参数类型实际上是安全的,但它们没有被发现有用,因此在实际语言中不被支持。”即使他们不支持,我很困惑,为什么这样的例子中,我们分别给予仍然是,从理论上讲,“安全”: class Animal {
...
public bool compare(Panda) { ... }
}
class Panda extends Animal {
private Dictionary<Type, List<IDataTransferObject>> dataStore = new Dictionary<Type, List<IDataTransferObject>>();
public void Insert<T>(T dto) where T : IDataTransferObject
{
if (!dataStore.C
假设我有接口和类: public interface ITree {}
public class Tree : ITree {}
由于IEnumerable<T>是协,下面的代码行成功编译: IEnumerable<ITree> trees = new List<Tree>();
但是,当我把它写成通用方法: public void Do<T>() where T : ITree
{
我想投一个逆变委托,但由于某种原因,我只能做使用“as”操作符吧。 interface MyInterface { }
delegate void MyFuncType<in InType>(InType input);
class MyClass<T> where T : MyInterface
{
public void callDelegate(MyFuncType<MyI