类和对象-point

创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方

法初始化x和y。创建类主类A来测试它。

 1 package com.hanqi.z1p1;
 2 
 3 public class Point {
 4 
 5     int X;
 6     int Y;
 7     void getX()
 8     {
 9         System.out.println("x的值为:"+X);
10     }
11     
12     void setX(int x)
13     {
14         X=x;
15         System.out.println("x的值为:"+X);
16     }
17     
18     
19     Point (int x,int y)
20     {
21         X=x;
22         Y=y;
23     }
24     
25     
26     public static void main(String[] args) {
27         // TODO 自动生成的方法存根
28 
29         Point p1=new Point(1,2);
30         
31         p1.getX();
32         p1.setX(50);
33         
34         
35     }
36 
37 }

运行结果:

原文地址:https://www.cnblogs.com/miss123/p/5512514.html