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

有关java 写一个父类,他的子类有长方形,三角形,圆
长方形有两个点坐标,三角形中有三个点坐标,圆有圆心和半径,怎么用多态来写,类中的方法有周长和面积

------解决方案--------------------
public class Point{

private long x ;
private long y ;

public Point(long x ,long y){
this.x = x ;
this.y = y ;
}
}

public abstract class Graphics{

public long perimeter() ;
public long area() ;
}

public class Rectangle extends Graphics{
private Point bPoint ;
private Point ePoint ;

public Rectangle(){}
public Rectangle(Point bPoint, Point ePoint){
this.bPoint = bPoint ;
this.ePoint = ePoint ;
}

public long perimeter() {
//自己计算了
}
public long area() {
//自己计算了
}
}

其他类类似 Rectangle

main(String[] args){
Graphics g = new Rectangle(new Point(0, 1), new Point(5,5)) ;
System.out.println(g.area() + "----" + g.perimeter()) ;
}
------解决方案--------------------
探讨
试过了,还有错 bPoint.x对不对
public class Point{

private long x ;
private long y ;

public Point(long x ,long y){
this.x = x ;
this.y = y ;
}
}
class Graphics{

public long perimeter(……