java 类和对象10

创建一个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     }

结果:

posted @ 2016-05-20 16:28  唐枫  阅读(179)  评论(0编辑  收藏  举报