Result : the value of X has been changed.
hehe : (10,20)
hehe : (30,20)
Sample Code
File name : Ch08Ex01.Java
public class Ch08Ex01 {
public static void main(String[] args) {
Points pt01 = new Points(10,20);
pt01.showPoint();
pt01.setX(30);
pt01.showPoint();
}
}
File name : Points.java
public class Points {
int x;
int y;
public Points (int new_x, int new_y){
this.x = new_x;
this.y = new_y;
}
public void setX(int x){
//x=x;
this.x=x;
}
public void showPoint(){
System.out.println("hehe : ("+x+","+y+")");
}
}
Applied Code 1, with Getter&Setter
File name : Ch08Ex01.Java
class Points{
private int x;
private int y;
public int getXX() {
return x;
}
public void setXX(int new_x) {
x = new_x;
}
public int getYY() {
return y;
}
public void setYY(int new_y) {
y = new_y;
}
};
public class Ch08Ex01 {
public static void main(String[] args) {
Points pt01 = new Points();
pt01.setXX(10);
pt01.setYY(20);
System.out.println("hehe : ("+pt01.getXX()+","+pt01.getYY()+")");
pt01.setXX(30);
System.out.println("hehe : ("+pt01.getXX()+","+pt01.getYY()+")");
}
}
Console of Applied Code 1
hehe : (10,20)
hehe : (30,20)
Reference book : 이클리스로 배우는 Java 6 Programming/ 임종권, 전병찬/ 가메출판사
댓글 없음:
댓글 쓰기