1. 自己设计一个坐标类,能提供以下方法如求当前坐标和其他坐标之间的距离等方法,要求所有变量为私有变量,并提供两个构造函数。
public class XYdistance {
private int x;
private int y;
XYdistance(){
setX(0);
setY(0);
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
XYdistance m_1= new XYdistance();
m_1.setX(10);
m_1.setY(10);
XYdistance m_2= new XYdistance();
double distance=(m_1.getX()-m_2.getX())*(m_1.getX()-m_2.getX())+(m_1.getY()-m_2.getY())*(m_1.getY()-m_2.getY());
double result=Math.sqrt(distance);
System.out.println(result);
}
}
编写使用静态变量统计一个类产生的实例对象的个数的程序?
public class Static {
private static int number;
public Static(){
//number=number+1;
++number;
//System.out.println(++number);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Static();
//m_1.Static();
System.out.println(Static.number);
}