如何在Java中使用泛型参数重载方法? [英] How to overload a method with generic parameter in java?

查看:960
本文介绍了如何在Java中使用泛型参数重载方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

通用重载的Java删除(而不是覆盖)

如何在Java中使用泛型参数重载方法?



假设我有以下类:

  import java.util.List; 

public class C {

public void foo(List< String> a){

}

public void foo(List
}

}

这给编译错误带来了错误,所以擦除是在做它的工作并删除类型信息,所以在编译之后,这两个方法只会有相同的列表参数。



如果我不想使用

  public void foo(List< T> a){

}

是否有其他方法来重载方法foo()?

$ b

解决方案


ol>

  • 更改方法名称。 使用一个接口来达到这个目的。


  • 然后将foo代码放置在对象本身中,或者使用
    double-dispatch(取决于您的设计paradi gm and taste)。

    使用对象中的代码:

      public interface FooInterface {
    void foo();
    }


    public class Foo
    {

    public< T extends FooInterface>无效abc(集合< T>对象)
    {
    for(T object:objects)
    {
    object.foo();



    $ b $ public class FooString implements FooInterface {

    public void foo(){
    // do foo for String


    $ b $ public class FooInteger implements FooInterface {

    public void foo(){
    // do foo for整数
    }

    }

    如果域包含您的逻辑。



    使用双重调度,您可以将FooInterface传递给foo方法,并根据类型调用适当的方法。 b $ b

    Possible Duplicate:
    Java erasure with generic overloading (not overriding)

    How to overloading method with generic parameter in java?

    Say I have following class

    import java.util.List;
    
    public class C {
    
        public void foo(List<String> a){
    
        }
    
        public void foo(List<Integer> b){
    
        }
    
    }
    

    This gives compilation error and rightly so as erasure is doing it's job and removing the type information, so after compilation both the methods will have same parameter of list only.

    If I don't want to use

    public void foo(List<T> a){
    
    }
    

    Is there any other way to overload the method foo()?

    解决方案

    Two approaches that you should follow.

    1. Change the method names.

    2. Use an Interface for this purpose.

    then either put the foo code in the objects themselves, or use double-dispatch (depending on your design paradigm and taste).

    With code in objects that would be:

    public interface FooInterface{
        void foo();
    }
    
    
    public class Foo
    {
    
        public <T extends FooInterface> void abc(Collection<T> objects)
        {
            for(T object: objects) 
            {
                object.foo();
            }           
        }
    }
    
    public class FooString implements FooInterface {
    
        public void foo(){
           // do foo for String
        }
    }
    
    public class FooInteger implements FooInterface {
    
        public void foo(){
          // do foo for Integer
        }
    
    }
    

    If Domain contains your logic.

    With a double dispatch, you would pass the "FooInterface" to the foo method, and on the handler call the appropriate method depending on the type.

    这篇关于如何在Java中使用泛型参数重载方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆