我有以下3个班级:
Order.java
public class Order<T> implements Comparator<File> {
T[] args;
public Order(T[] args) {
this.args = args;
}
@Override
public int compare(File o1, File o2) {
return 0;
}
}
FileList.java:
public class FileList extends LinkedList<File> {
public FileList() {
this.add(new File("example.txt"));
}
public void printByOrder(Order order) {
this.stream().sorted(order)
.forEach(file -> System.out.println(file.getName()));
}
}
最后,Test.java:
public class Test {
public static void main(String[] args) {
FileList fileList = new FileList();
Integer[] arguments = {1, 2, 3};
Order<Integer> order = new Order<>(arguments);
fileList.printByOrder(order);
}
}
我在以下行中收到错误:
this.stream().sorted(order)
.forEach(file -> System.out.println(file.getName()));
,指出:
Error:(12, 57) java: cannot find symbol
symbol: method getName()
location: variable file of type java.lang.Object
我理解订单有原始类型是个问题。例如,将printByOrder
更改为:
public void printByOrder(Order<Integer> order) {
this.stream().sorted(order)
.forEach(file -> System.out.println(file.getName()));
}
修复了错误,但我希望能够将包含任何类型变量的Order
对象发送到此方法。
这样做的正确方法是什么?