我写的求两点间的距离
没看到报错 但是运行的结果是空指针 为什么?
点类
package mxdx;
public class D {
// 构造函数
public D(float x, float y)
{
this.x=x;
this.y=y;
}
private float x,y ;
float getY() {return y;}
float getX() {return x;}
void set(float x,float y )
{
this.x=x;
this.y=y;
}
void show()
{
System.out.println("("+x+","+y+")");
}
}
直线类
package mxdx;
public class Zx {
//静态成员
D p1,p2;
float distance;
float disx,disy;
void set(D p1,D p2)
{
this.p1=p1;
this.p2=p2;
}
void calc()
{
float disx=p1.getX()-p2.getX();
float disy=p1.getY()-p2.getY();
distance=(float)Math.sqrt((disx*disx)+(disy*disy));
}
static float getDis(D p1,D p2) {
float distance;
float disx=p1.getX()-p2.getX();
float disy=p1.getY()-p2.getY();
distance=(float)Math.sqrt(disx*disx+disy*disy);
return distance;
}
void show()
{
p1.show();
p2.show();
System.out.println("两点间的距离:"+distance);
}
}
运行代码
package mxdx;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
D d1=new D(2,3);
D d2=new D(2,3);
Zx eg=new Zx();
Zx.getDis(d1,d2);
eg.show();
}
}
运行截图