代码之家  ›  专栏  ›  技术社区  ›  Ilya Gazman

如何泛型地调用泛型方法?

  •  0
  • Ilya Gazman  · 技术社区  · 6 年前

    public interface ByteConsumer{
    
        // Method 1
        default <T> T consume(T consumer) throws IOException{
            return consume(consumer);
        }
    
        // Method 2
        default <T extends BaseToBytes> T consume(T consumer) throws IOException {
            consume(consumer.getInputStream());
            return consumer;
        }
    
        void consume(InputStream input) throws IOException;
    }
    
    1 回复  |  直到 6 年前
        1
  •  -1
  •   Ilya Gazman    6 年前

    @Override
    default <T> T consume(T consumer) throws IOException{
        //noinspection unchecked
        return (T) consume((BaseToBytes)consumer);
    }