（1）在点类内访问属性和方法
package ch05_15;
public class Point {
	private double x;
	private double y;
	//带参数的构造方法
	public Point(double x, double y) {
		this.x = x;
		this.y = y;
	}
	public Point() {}//默认构造方法
	public double getX() {
		return this.x;
	}
	public void setX(double x) {
		this.x = x;
	}
	public double getY() {
		return this.y;
	}
	public void setY(double y) {
		this.y = y;
	}
	//计算两点之间的距离，一个点是this，另一个点作为参数
	public double distance(Point p){
		double s;
		s=Math.sqrt(( this.x-p.x )*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
		return s;
	}
}
（2）在主类内，通过点对象访问约束的属性和方法
package ch05_15; 
public class MainPoint {
	public static void main(String[] args) {
		Point p1,p2;  //声明两个点；
		double d;    //存放两点之间的距离变量
		p1=new Point(1,1);
		p2=new Point(10,10);
		d=p1.distance(p2);
		System.out.println("p1点的坐标为：("+p1.getX()+","+p1.getY()+")");
		System.out.println("p2点的坐标为：("+p2.getX()+","+p2.getY()+")");
          //下面的语句是错误的，不允许访问私有成员
		//System.out.println("p2点的坐标为：("+p2.x+","+p2.y+")");/ 
		System.out.println("两点距离为为："+d);
	}
}
