阅读MyPoint.java,在main()方法里添加语句完成如下功能
- 创建一个MyPoint类对象myPoint。
- 使用toString()方法,输出myPoint的信息,体验类方法的覆盖,在本例中MyPoint类覆盖了Object的方法toString()方法。
- 为myPoint对象中的x和y属性分别赋值6、7,体验在Java中类的封装是如何实现的;设置myPoint的大小为3。
代码:
MyPoint.java
public class MyPoint {
private int x;
private int y;
public int size;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String toString() {
this.x = 6;
this.y = 7;
return "["+this.x+","+this.y+"]";
}
}
TestPoint.java
public class TestPoint {
public static void main(String[] args) {
MyPoint myPoint = new MyPoint();
myPoint.setX(1);
myPoint.setY(2);
myPoint.toString();
}
}