0

我正在尝试扩展“接口的交集”。所以:我下面的例子是试图找到超接口RandomAccessFile (Implemented Interfaces: Closeable, DataInput, DataOutput, AutoCloseable )DataInputStream(Implemented Interfaces: Closeable, DataInput, AutoCloseable )因此我们至少在Closeable和上有一个交集DataInput(当然我可以与AutoCloseable):所以代码是这样的:

private static <I extends DataInput & Closeable> Person read(I source) {
    try (I input = source) {
        return new Person(input.readLine(), Integer.valueOf(input.readLine()));         
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

当我这样做时,它的效果很好:

public static void main(String[] args) throws IOException {  
    RandomAccessFile input = new RandomAccessFile("src\\main\\resorces\\person.txt", "rw");
    Person person2 = read(input);
    System.out.println(person2.toString());

}

但是当我这样做时:

public static void main(String[] args) throws IOException {     
    DataInputStream stream = new DataInputStream(new
        FileInputStream("src\\main\\resorces\\person.txt")); 
    Person person = read(stream); 
    System.out.println(person.toString());       

}

它向我抛出了一个异常:

Exception in thread "main" java.lang.NoSuchMethodError: `java.io.DataInput.close()V
at com.test.java8.l03.generics.Person.read(Person.java:34)
at com.test.java8.l03.generics.Person.main(Person.java:20)

以下是所有代码:

public class Person {

    public static void main(String[] args) throws IOException {

        /*DataInputStream stream = new DataInputStream(new FileInputStream("src\\main\\resorces\\person.txt"));
        Person person = read(stream);
        System.out.println(person.toString());*/


        RandomAccessFile input = new
          RandomAccessFile("src\\main\\resorces\\person.txt", "rw"); 
        Person person2 = read(input); System.out.println(person2.toString());
    }

    private static <I extends DataInput & Closeable> Person read(I source) {
        try (I input = source) {
            return new Person(input.readLine(), Integer.valueOf(input.readLine()));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    private static Person read(RandomAccessFile source) {
        try (RandomAccessFile input = source) {
            return new Person(input.readLine(), Integer.valueOf(input.readLine()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;

    }
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}

我不会想到这个错误,因为我扩展了 Closeable 的相交接口。谁能帮我这个?

但是,让接口置换不会引发异常:

private static <I extends Closeable & DataInput> Person read(I source) {
try (I input = source) {
    return new Person(input.readLine(), Integer.valueOf(input.readLine()));
} catch (IOException e) {
    e.printStackTrace();
    return null;
}

}

接口的交集是否可能不是等价关系(更准确地说,该操作数上的交集不是自反的)并且编译器将左侧接口作为返回值?

与此相关:DataInputStream 实现了 DataInput 并扩展了实现 Closeable 的抽象类 InputStream。RandomAccessFile 直接实现 DataInput 和 Closeable 因此 RandomAccessFile 必须实现方法 close() 而 DataInputStream 没有(它可以使用实现 Closeable 接口的超类中的方法)?因此,当使用动态方法分派时,编译器可能会在这里感到困惑???

4

0 回答 0