qed*_*qed 4 java constructor overloading
这是代码:
import java.util.ArrayList;
import java.util.List;
/**
* Created by IDEA on 15/11/14.
*/
public class Matrix<T> {
private final int nrow;
private final int ncol;
Matrix(List<List<T>> data) {
nrow = data.size();
ncol = data.get(0).size();
}
Matrix(List<T[]> data) {
nrow = data.size();
ncol = data.get(0).length;
}
}
Run Code Online (Sandbox Code Playgroud)
IDE要求我删除第一个构造函数,为什么?
Jon*_*eet 14
IDE要求我重命名或删除第一个构造函数,为什么?
这是因为泛型在Java中的工作方式.由于类型擦除,你有效地声明了两个这样的构造函数:
Matrix(List data)
Run Code Online (Sandbox Code Playgroud)
这就是JVM看到的内容,其中添加了一些关于所涉及的类型参数的元数据.
这不仅适用于构造函数 - 您也不能基于泛型类型参数重载方法.
有关可怕的详细信息,请参阅类型擦除的Java Generics FAQ条目.
奇怪的是你的构造函数只需要行和列数,但这是一个使用静态工厂方法的版本:
import java.util.ArrayList;
import java.util.List;
public class Matrix<T> {
private final int nrow;
private final int ncol;
private Matrix(int nrow, int ncol) {
this.nrow = nrow;
this.ncol = ncol;
}
public static <T> Matrix<T> fromArrays(List<T[]> data) {
return new Matrix<T>(data.size(), data.get(0).length);
}
public static <T> Matrix<T> fromLists(List<? extends List<T>> data) {
return new Matrix<T>(data.size(), data.get(0).size());
}
}
Run Code Online (Sandbox Code Playgroud)
并使用此演示:
public class Test {
public static void main(String[] args) {
// We're only checking the compilation for the moment :)
List<List<String>> lists = null;
List<String[]> arrays = null;
Matrix<String> x = Matrix.fromArrays(arrays);
Matrix<String> y = Matrix.fromLists(lists);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
975 次 |
| 最近记录: |