当前位置: 代码迷 >> Java相关 >> 类的继承与多太
  详细解决方案

类的继承与多太

热度:79   发布时间:2007-04-17 08:38:47.0
类的继承与多太
abstract class Shape
{
protected abstract Point prostion(float x,float y);
public abstract void move(float x,float y);
public abstract void show();
}
class Point
{
private float x;
private float y;
Point(float x,float y)
{
this.x=x;
this.y=y;
}
public void setX(float x)
{
this.x=x;
}
public void setY(float y)
{
this.y=y;
}
public float getX()
{
return x;
}
public float getY()
{
return y;
}
public String toString()
{
return x+","+y;
}
}
public class Line extends Shape
{
private Point start;
private Point end;
Line(Point start,Point end)
{
this.start=start;
this.end=end;
}
protected Point prostion(float x,float y)
{
return start;
}
public void show()
{
System.out.println("起点"+start.toString()+"终点"+end.toString()+"直线长"+getLang());
}
public String toString()
{
return "起点"+start+"终点"+end;
}
public double getLang()
{
return Math.sqrt(Math.pow((end.getX()-start.getX()),2)+Math.pow((end.getY()-start.getY()),2));
}
/*public String toString()
{
return "起点"+start.toString()+"终点"+end.toString()+"直线长"+getLang();
}*/ //这个方法为什么不能重写?????
public void move(float x,float y)
{
start.setX(start.getX()+x);
end.setX(end.getX()+x);
start.setY(start.getY()+y);
end.setY(end.getY()+y);
}
}
class Circle extends Shape
{
private Point center;
private float r;
protected Point prostion(float x,float y)
{
return center;
}
public void show()
{}
Circle(Point center,float r)
{
this.center=center;
this.r=r;
}
public void setCenter(Point center)
{
this.center=center;
}
public void setR(float r)
{
this.r=r;
}
public Point getCenter()
{
return center;
}
public float getR()
{
return r;
}
public void move(float x,float y)
{
center.setX(center.getX()+x);
center.setY(center.getY()+y);
}
public String toString()
{
return "圆心坐标"+center+"半径"+r;
}
}
public class P263E1
{
public static void main(String args[])
{
Line line=new Line(new Point(6,3),new Point(8,9));
//System.out.println(line.toString()); // 这个方法为什么不能用?
line.show();
Circle circle=new Circle(new Point(6,8),8);
System.out.println(circle.toString());
line.move(4,8);
System.out.println("直线移动后:");
line.show(); //移动后直线长为什么没变
circle.move(6,15);
System.out.println("圆移动后:");
System.out.println(circle.toString());
}
}
搜索更多相关的解决方案: 继承  

----------------解决方案--------------------------------------------------------
怎么没人回答我啊?!
----------------解决方案--------------------------------------------------------
  相关解决方案