Tag: generics

为什么这个通用扩展方法不能编译?

代码有点奇怪,请耐心等待(请记住,这种情况确实出现在生产代码中)。 假设我有这个接口结构: public interface IBase { } public interface IChild : IBase { } public interface IFoo<out T> where T : IBase { } 使用这个扩展方法类来构build接口: public static class FooExt { public static void DoSomething<TFoo>(this TFoo foo) where TFoo : IFoo<IChild> { IFoo<IChild> bar = foo; //foo.DoSomethingElse(); // Doesn't compile — why not? bar.DoSomethingElse(); // OK […]

用Java中的通用参数重写一个方法?

我有一个抽象的类Monitor.java是由一个类EmailMonitor.java分类 。 方法: public abstract List<? extends MonitorAccount> performMonitor(List<? extends MonitorAccount> accounts) 在Monitor.java中定义,并且必须在EmailMonitor.java中重写。 我目前已经在EmailMonitor.java中重写了如下的方法: @Override public List<EmailAccount> performMonitor(List<EmailAccount> emailAccounts) { //…unrelated logic return emailAccounts; } 但是,这会产生编译时错误: Name clash: The method performMonitor(List<EmailAccount>) of type EmailMonitor has the same erasure as performMonitor(Lis<? extends MonitorAccount> emailAccounts) of type Monitor but does not override it EmailAccount是EmailAccount的子类,所以(至less在我看来)以这种方式覆盖它是非常有意义的。 看到编译器对我的逻辑不满意,我该如何解决这个问题,同时还要保持我的编译时间检查,以确保对EmailMonitor.performMonitor()所有调用都接收EmailAccount列表,而不是其他types的MonitorAccount ?

如何将List <object>转换为List <SomethingElse>

我怎样才能将List<object>为List<SomethingElse> ? ( SomethingElse已知从object下降 ) 奖金喋喋不休 投射清单: List<Object> first = …; List<SomethingElse> second = (List<SomethingElse>)first; 不起作用: 无法将types“System.Collections.Generic.List”转换为“System.Collections.Generic.List” 投射清单: List<SomethingElse> second = first.Cast<SomethingElse>(); 不起作用: 不能隐式地将types“System.Collections.Generic.List”转换为“System.Collections.Generic.List” 我实际上并不需要完整的List<T>对象,只是一个ICollection<T>会做: ICollection<SomethingElse> second = first; ICollection<SomethingElse> second = (ICollection<SomethingElse>)first; ICollection<SomethingElse> second = first.Cast<SomethingElse>(); 不工作。

特定基类的C ++类模板

比方说,我有类: class Base{}; class A: public Base{ int i; }; class B:public Base{ bool b; }; 现在我想定义一个模板类: template < typename T1, typename T2 > class BasePair{ T1 first; T2 second; }; 但是我想定义它,只有类Base的后代可以用作模板参数。 我怎样才能做到这一点?

通用的多对多关系

我正在尝试创build一个邮件系统,邮件的发件人和收件人可以是通用实体。 这似乎罚款的发件人,只有对象引用(GenericForeignKey),但我不知道如何去接收者(GenericManyToManyKey ??) 下面是一个简单的例子。 PersonClient和CompanyClient从客户端inheritance属性,但有自己的具体细节。 最后一行是棘手的问题。 如何让消息收件人成为一组公司客户端和个人客户端 class Client(models.Model): city = models.CharField(max_length=16) class Meta: abstract = True class PersonClient(Client): first_name = models.CharField(max_length=16) last_name = models.CharField(max_length=16) gender = models.CharField(max_length=1) class CompanyClient(Client): name = models.CharField(max_length=32) tax_no = PositiveIntegerField() class Message(models.Model): msg_body = models.CharField(max_length=1024) sender = models.ForeignKey(ContentType) recipients = models.ManyToManyField(ContentType)

IEnumerable和IEnumerable <T>之间的区别?

IEnumerable和IEnumerable<T>什么区别? 我见过很多实现这两个接口的框架类,所以我想知道通过实现这两个接口有什么好处? 请看看他们是如何定义的: public interface IEnumerable { [DispId(-4)] IEnumerator GetEnumerator(); } public interface IEnumerable<T> : IEnumerable { IEnumerator<T> GetEnumerator(); } 正如我们所看到的, IEnumerable<T>从IEnumerable派生,这意味着无论IEnumerable具有什么, IEnumerable<T>inheritance,那么为什么我们要实现而不仅仅是IEnumerable<T>呢? 正在实现IEnumerable<T>不够? 同样,还有其他类似的配对: IList和IList<T> ICollection和ICollection<T> 我也想知道这些。

比较一个generics与空值可能是一个值或引用types?

public void DoFoo<T>(T foo) where T : ISomeInterface<T> { //possible compare of value type with 'null'. if (foo == null) throw new ArgumentNullException("foo"); } 我故意只检查null,因为我不想限制ValueType等于它的default(T) 。 我的代码编译和工作就这么好(ReSharper抱怨,但不是CodeAnalysis)。 虽然我想知道: 有没有更为标准的方法来处理这种情况? 有没有可能由此产生问题? 当我拨打电话并通过值types时,真正发生了什么?

将整数列表分配到string列表中

我正在学Java的generics,我接近了一个非常有趣的代码。 我知道在Java中将一种types的列表添加到另一种types是非法的。 List<Integer> integerList = new ArrayList<Integer>(); List<String> stringList=integerList; 所以在第二行我得到一个编译时错误。 但是如果我在这样的类中创build一个generics方法, class GenericClass <E>{ void genericFunction(List<String> stringList) { stringList.add("foo"); } // some other code } 而在主类调用与Integer列表的方法我没有得到任何错误。 public class Main { public static void main(String args[]) { GenericClass genericClass=new GenericClass(); List<Integer> integerList= new ArrayList<Integer>(); integerList.add(100); genericClass.genericFunction(integerList); System.out.println(integerList.get(0)); System.out.println(integerList.get(1)); } } 产量 100 FOO 为什么我没有得到任何错误?

如何实例化一个在Spring框架中使用generics的对象?

我有一个看起来像这样的类: class Dao<T>{ … } 我想做这个: new Dao<Student>(); 来自Spring XMLconfiguration。 可以这样做吗? 怎么样?

generics/模板在Python?

python如何处理generics/模板types的场景? 说我想创build一个外部文件“BinaryTree.py”,并处理二叉树,但任何数据types。 所以我可以将它传递给一个自定义对象的types,并有一个该对象的二叉树。 这是怎么做到的Python?