我不确定如何运行通用类型Point的方法。假设以下类通用类型的运行方法
class Point1 {
double x, y;
public Point1 (double x_, double y_) {x=x_; y = y_;}
public double getX() {return x;}
public double getF1() {return x;}
}
class Point2 {
double lat, lon;
public Point2 (double lat_, double lon_) {lat = lat_; lon = lon_;}
public double getLat(){return lat;}
public double getF1() {return lat;}
}
共享相同的方法getF1()和一个方法
public <Point> void test(List<Point> points) {
for (Point point:points)
double x = point.getF1(); //Error Can not find symbol getF1()
}
public static void main (String [args]) {
List <Point1> points = new ArrayList<>();
test(points);
}
如何运行与POINT1类型为普通类型点(POINT = POINT1相关联的方法getF1() )?是否有可能使用的接口
public interface ICoord {
double f();
和
public <Point> void test(List<Point> points, ICoord function) {
for (Point point:points)
double x = point.function.f();
}
'Point1'和'Point2'必须有一些通用的超类或接口才能够一般地引用它们。 – Taylor
什么是'Point'?没有看到它的定义。 –
@Sabir:这里Point = Point1 – justik