题解 | #修改Data类的定义#
修改Data类的定义
https://www.nowcoder.com/practice/bb89f943588c4a248675881f2e5cc761
// 复习了一下对于构造方法为 private 构建对象的方法 // 1.写一个公共静态方法 在其中返回实例对象 // 2.反射 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int x = scanner.nextInt(); int y = scanner.nextInt(); Data data = Data.getInstance(); data.setX(x); data.setY(y); System.out.println(data.getX() + data.getY()); } } } class Data { private static Data instance; private int x; private int y; private Data() {} public static Data getInstance (){ if(instance == null){ instance = new Data(); } return instance; } public int getX() { return x; } public int getY() { return y; } public void setX(int xnum) { x = xnum; } public void setY(int ynum) { y = ynum; } }