繁体   English   中英

Java通用类型实例化

[英]Java Generic type instantiation

像这样的问题很多,但是似乎没有一个问题可以专门回答我的问题。

您如何实例化新的T?

我有一个通用方法,我需要在type参数中返回该类型的新实例。 这是我的代码...

class MyClass {

  public static MyClass fromInputStream( InputStream input ) throws IOException {

    // do some stuff, and return a new MyClass.

  }
}

然后在一个单独的类中,我有一个像这样的通用方法...

class SomeOtherClass {

  public <T extends MyClass>download(URL url) throws IOException {

    URLConnection conn = url.openConnection();

    return T.fromInputStream( conn.getInputStream() );

  }
}

我也尝试了以下...

class SomeOtherClass {

  public <T extends MyClass>download(URL url) throws IOException {

    URLConnection conn = url.openConnection();

    return new T( conn.getInputStream() ); // Note my MyClass constructor takes an InputStream...

  }
}

但是以上两种排列都不会编译! 错误是:

File: {...}/SomeOtherClass.java
Error: Cannot find symbol
symbol : class fromInputStream
location : class MyClass

任何建议,将不胜感激!

我认为一种常见的方法是要求像这样传递T类型的类:

class SomeOtherClass {

  public <T extends MyClass> T download(Class<T> clazz, URL url) throws IOException {

    URLConnection conn = url.openConnection();

    return clazz.getConstructor(InputStream.class).newInstance(conn.getInputStream() ); // Note my MyClass constructor takes an InputStream...

  }
}

除了传递Class对象并像johncarl的答案中那样使用反射之外 ,还可以使用泛型工厂:

public abstract class InputStreamFactory<T> {

    public T make(InputStream inputStream) throws IOException;
}

并修改download

public <T extends MyClass> T download(URL url, InputStreamFactory<? extends T> factory) throws IOException {

    URLConnection conn = url.openConnection();

    return factory.make(conn.getInputStream());
}

每个MyClass派生都可以提供自己的工厂实现:

public class MySubClass extends MyClass {

    public static final InputStreamFactory<MySubClass> FACTORY =
            new InputStreamFactory<MySubClass>() {
                @Override
                public MySubClass make(InputStream inputStream) throws IOException {
                    return new MySubClass(inputStream); //assuming this constructor exists
                }
            };
}

呼叫者可以引用它:

MySubClass downloaded = new SomeOtherClass().download(url, MySubClass.FACTORY);

您无需在此处使用参数来调用方法。 因为您有静态方法,足以直接从MyClass访问fromInputStream方法,我的意思是:

return MyClass.fromInputStream( conn.getInputStream() );

希望对您有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM