創建一個Point類,有成員變量x,y,方法getX(),setX(),還有一個構造方法初始化x和y。創建類主類A來測試它。
1 public class Print { 2 3 private int x; 4 private int y; 5 6 public int getX() { 7 return x; 8 } 9 10 public void setX(int x) { 11 this.x = x; 12 } 13 14 Print(int num1, int num2) { 15 x = num1; 16 y = num2; 17 } 18 19 public static void main(String[] args) { 20 Print p1 = new Print(3, 4); 21 System.out.println("x=" + p1.getX() + "\ty=" + p1.y); 22 23 p1.setX(15); 24 System.out.println("x=" + p1.getX() + "\ty=" + p1.y); 25 26 }
結果: