public class Point { private T x; private T y;

public Point(T x, T y) {
    this.x = x;
    this.y = y;
}

public T getX() {
    return x;
}

public void setX(T x) {
    this.x = x;
}

public T getY() {
    return y;
}

public void setY(T y) {
    this.y = y;
}

}

// Example usage Point point = new Point<>(10, 20); System.out.println("x: " + point.getX()); System.out.println("y: " + point.getY());

Point point2 = new Point<>(3.5, 4.2); System.out.println("x: " + point2.getX()); System.out.println("y: " + point2.getY())

用Java写一个类需求设计-个点类 Point其中设计字段x y;希望在设计类的时候其中的数据类型我们就写一个符号泛指所有的类型但是不指具体的某种类型当有人使用我们上面的模板来创建一个对象的时候他们自己可以指定自 己的类型

原文地址: https://www.cveoy.top/t/topic/ipE8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录