题解 | #修改Data类的定义#
修改Data类的定义
http://www.nowcoder.com/practice/bb89f943588c4a248675881f2e5cc761
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 = new Data(x, y);
System.out.println(data.getX() + data.getY());
}
}
}
class Data {
private int x;
private int y;
public Data(int x, int y) {//把构造方法的访问属性改为public,这样才能调用构造方法实例化一个对象
this.x = x;//如果没有this.x,则相当于把形参x的值赋值给参数x(即自己给自己赋值,并不能将值传递给对象的x值)
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}