Java泛型编程:使用泛型创建灵活的Point类
public class Point<T> { 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<Integer> point = new Point<>(10, 20); System.out.println("x: " + point.getX()); System.out.println("y: " + point.getY()); Point<Double> point2 = new Point<>(3.5, 4.2); System.out.println("x: " + point2.getX()); System.out.println("y: " + point2.getY());
原文地址: https://www.cveoy.top/t/topic/p7p4 著作权归作者所有。请勿转载和采集!