定义一个类LOCATION,用数据成员x,y表示该类对象在二维坐标系的坐标位置用函数成员move移动对象坐标位置,然后以类LOCATION为基类派生出点类POINT再利用点类POINT派生出圆类CIRCLE.定义点类和圆类
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 01:43:05
定义一个类LOCATION,用数据成员x,y表示该类对象在二维坐标系的坐标位置用函数成员move移动对象坐标位置,然后以类LOCATION为基类派生出点类POINT再利用点类POINT派生出圆类CIRCLE.定义点类和圆类
定义一个类LOCATION,用数据成员x,y表示该类对象在二维坐标系的坐标位置
用函数成员move移动对象坐标位置,然后以类LOCATION为基类派生出点类POINT再利用点类POINT派生出圆类CIRCLE.定义点类和圆类相应的move和draw函数.在主函数中定义各类的对象并调用成员函数以测试功能.
非常感谢高手的指点哈~
定义一个类LOCATION,用数据成员x,y表示该类对象在二维坐标系的坐标位置用函数成员move移动对象坐标位置,然后以类LOCATION为基类派生出点类POINT再利用点类POINT派生出圆类CIRCLE.定义点类和圆类
public abstract class Location {
public abstract double getX();
public abstract double getY();
public abstract void move(double x,double y);
public abstract void draw();
}
public class Point extends Location {
private double x;
private double y;
public Point() {
\x09this.x = 0;
\x09this.y = 0;
}
public Point(double x,double y) {
\x09this.x = x;
\x09this.y = y;
}
@Override
public double getX() {
\x09return x;
}
@Override
public double getY() {
\x09return y;
}
@Override
public void move(double x,double y) {
\x09this.x = x;
\x09this.y = y;
\x09
\x09System.out.println("move to Point:(" + this.getX() + "," + this.getY() + ")");
}
@Override
public void draw() {
\x09System.out.println("draw positon:(" + this.getX() + "," + this.getY() + ")");
\x09
}
}
public class Circle extends Location {
private Point point;
private double radius;
public Circle() {
\x09this.point = new Point();
\x09radius = 10;
}
public Circle(Point point,double radius) {
\x09this.point = point;
\x09this.radius = radius;
}
@Override
public double getX() {
\x09return point.getX();
}
@Override
public double getY() {
\x09return point.getY();
}
@Override
public void move(double x,double y) {
\x09this.point.move(x,y);
}
@Override
public void draw() {
\x09System.out.println("draw positon:Point (" + this.getX() + "," + this.getY() + "),radius= " + this.getRadius());
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
测试类
public class MyTest {
public static void main(String[] args) {
\x09Point p1 = new Point();
\x09Point p2 = new Point(88,86.55);
\x09p1.draw();
\x09p1.move(120.87,66.34);
\x09p2.draw();
\x09p2.move(666,777);
\x09
\x09Circle c1 = new Circle();
\x09c1.draw();
\x09c1.move(22,33);
\x09Circle c2 = new Circle(new Point(55,66),33);
\x09c2.draw();
\x09c2.move(888,999);
}
}
此处的draw不是真的画图哦