———————————————————————————————————
// 测试用
public static void main(String[] args){
Point p1,p2;
Circle c;
p1=new Point(3,2);
c=new Circle(p1,5);
System.out.println("center of c is:
"+c.getCenter().getX()+","+c.getCenter().getY());
p2=new Point(5,6);
c.setCenter(p2);
System.out.println("center of c changed to: "+c.getCenter().getX()+","+c.getCenter().getY());
}
}
Java 程序设计实验(NO.3)
实验目的:理解方法的重载、重写,掌握抽象类、接口和多态的使用。
1、定义矩形接口 Rect,在接口中有计算面积 area()、获取宽度 getWidth()、获取高度、设置矩形
某端点位置等,继承接口 Rect 实现矩形 Rectangle,包含四个成员变量,分别定义该矩形的左上角和右
下角的坐标,lefttop_x、lefttop_y、rightdown _x、rightdown_y。要求设计三个同名的不同 buildRect()方
法为这些实例变量设置值。最后给出一个测试类。
提示:正确使用 API 中 Point 类的成员变量和方法。
import java.awt.Point;
interface Rect{
int area();
int getWidth();
int getHeight();
}
class MyRect implements Rect{
private int topLeft_x,topLeft_y;
private int bottomRight_x,bottomRight_y;
MyRect buildRect(int x1,int y1,int x2,int y2){
topLeft_x=x1;
topLeft_y=y1;
bottomRight_x=x2;
bottomRight_y=y2;
return this;
}
MyRect buildRect(Point topLeft,Point bottomRight){
topLeft_x=topLeft.x;
topLeft_y=topLeft.y;
bottomRight_x=bottomRight.x;
bottomRight_y=bottomRight.y;