日期:2014-05-20  浏览次数:20824 次

急 简单地java问题
class Point{
  private double x,y;
  public Point(double ex,double ey){
  x=ex;
  y=ey;
  }
  public static double getInstance(Point p1, Point p2) {
  return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
  }
}
class Line {
Point n1,n2;
public Line(Point n1,Point n2){
this.n1=n1;
this.n2=n2;
}
public String Length( ){
return "length is :"+ Point.getInstance(n1, n2);
}
}
class Triangel{
Point n1,n2,n3;
public Triangel(Point n1, Point n2,Point n3){
this.n1=n1;
this.n2=n2;
this.n3=n3;  
}
public double getperimeter(){
return Point.getInstance(n1,n2)+Point.getInstance(n1,n3)+Point.getInstance(n2,n3);
}
public double getarea(){
double a=Point.getInstance(n1,n2);
double b=Point.getInstance(n1,n3);
double c=Point.getInstance(n2,n3);
double s=(a+b+c)/2;
double area=Math.sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
}
class Rectangale{
Point n1,n2,n3,n4;
public Rectangale(Point n1,Point n2,Point n3,Point n4){
this.n1=n1;
this.n2=n2;
this.n3=n3;
this.n4=n4;
}
public double getperimeter(){
return 2*Point.getInstance(n1,n2)+2*Point.getInstance(n1,n3);
}
public double getarea(){
return Point.getInstance(n1,n2)*Point.getInstance(n1,n3);
}
}
class Circle{
Point n1;
double r;
public Circle(Point n1,double r){
this.n1=n1;
this.r=r;
}
public double getperimeter(){
return 2*Math.PI*r;
}
public double getarea(){
return Math.PI*r*r;
}
}

public class shiyan2{
public static void main(String[] args){
Point n1=new Point(0,0);

Circle e=new Circle(n1,5);
System.out.println(e.getarea());
}
}

上边 Line与Point的关系是不是继承关系,父类与子类的关系?

------解决方案--------------------
不是,同级关系
继承需要extends关键字。

比如
class Point extends Line{
}
才说明Line是Point的父类。

------解决方案--------------------
是引用!