题目描述
- 定义如下的类:
- Point类:用于描述坐标系中的一个点
- 属性:x、y,int类型,分别描述x坐标值和y坐标值;
- 方法:
- int distanceTo(Point p):计算两点之间的距离;
- Point centerPoint(Point p):求两点之间的中心点;
- Graphic抽象类:描述图形信息
- 属性:color(String类型,图形颜色)
- 方法:
- 构造方法:2个
- 无参的(颜色为空)
- 有参的(根据参数设置颜色信息)
- void printInfo():显示图形信息
- 抽象方法:double getArea() 功能:计算图形面积
- 抽象方法:double getCircum() 功能:计算图形周长
- 构造方法:2个
- Rect类:继承了Graphic类,用于描述矩形图形
- 添加属性: lefttop和rightbottom,均为Point类型,分别描述矩形的左上角和右下角点的坐标信息;
- 方法:
- 构造方法3个;
- 无参格式:利用基类无参构造方法设置基类部分信息,将两个点均设置为坐标原点;
- Rect(String color, Point lefttop, Point rightbottom)
- Rect(String color, int x_left, int y_top, int x_right, int y_bottom)
- 重写方法:getArea(),getCircum(),printInfo() 根据矩形信息进行相关处理;
- 添加方法:
- int getHeight(),求出矩形的高;
- int getWidth(),求出矩形的宽;
- boolean isRect():基于两点信息判断是否构成矩形;
- 构造方法3个;
- Circle类继承了Graphic类,用于描述圆形图形
- 添加属性:
- center,为Point类型,描述圆形的圆心;
- r,int类型,描述圆形半径
- 方法:
- 构造方法:根据实际需要,自行设定;
- 重写方法:getArea(),getCircum(), printinfo()根据圆形信息进行相关处理;
- 添加方法:
- void setinfo (string color, point center, int r):根据参数设置圆形的属性
- void changeSize(int):根据参数缩放圆形大小(提示:对半径进行修改)
- 添加属性:
- 定义主程序类,分别创建矩形对象和圆形对象,并对其操作验证类定义是否正确;同时练习向上转型和向下转型,加深对面向对象的多态性的理解。
核心思想
- 方法重写:子类继承父类,子类中重写父类方法,方法名,参数和返回值一样,方法内部实现不一样。
- 注意:
- 重写的方法必须要和父类一模一样(包括返回值类型,方法名,参数列表)
- 重写的方法可以使用@Override注解来标识
- 子类中重写的方法的访问权限不能低于父类中方法的访问权限
代码实现
- Point.java
public class Point {
double x;
double y;
public Point() {
this.x = 0;
this.y = 0;
}
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double d) {
this.x = d;
}
public double getY() {
return y;
}
public void setY(double d) {
this.y = d;
}
/**
* 两点之间的距离
*
* @param p
* @return
*/
double distanceTo(Point p) {
return (int) (Math.sqrt(Math.pow((this.x - p.x), 2)) + Math.pow((this.y - p.y), 2));
}
/**
* 中心点
*
* @param p
* @return
*/
Point centerPoint(Point p) {
Point midPoint = new Point();
midPoint.setX((this.x + p.x) / 2);
midPoint.setY((this.y + p.y) / 2);
return midPoint;
}
}
- Graphic.java
public abstract class Graphic {
String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Graphic() {
}
public Graphic(String color) {
this.color = color;
}
void printInfo() {
System.out.println("color="+this.color);
}
abstract double getArea();
abstract double getCircum();
}
- Rect.java
public class Rect extends Graphic {
Point lefttop;
Point rightbottom;
public Point getLefttop() {
return lefttop;
}
public void setLefttop(Point lefttop) {
this.lefttop = lefttop;
}
public Point getRightbottom() {
return rightbottom;
}
public void setRightbottom(Point rightbottom) {
this.rightbottom = rightbottom;
}
public Rect() {
super();
this.lefttop = new Point();
this.rightbottom = new Point();
}
public Rect(String color, Point lefttop, Point rightbottom) {
this.color = color;
this.lefttop = lefttop;
this.rightbottom = rightbottom;
}
public Rect(String color, int x_left, int y_top, int x_right, int y_bottom) {
this.color = color;
this.lefttop = new Point(x_left,y_top);
this.rightbottom = new Point(x_right,y_bottom);
}
@Override
double getArea() {
// 面积 = 宽*高
return this.getWidth() * this.getHeight();
}
@Override
double getCircum() {
// 周长=2 *(宽 + 高)
return 2 * (this.getWidth() + this.getHeight());
}
@Override
void printInfo() {
if (isRect()) {
System.out.println("宽:" + this.getWidth() + " 高:" + this.getHeight() + " 周长:" + this.getCircum() + " 面积:"
+ this.getArea() + " 颜色:" + this.color);
}else {
System.out.println("这不是长方形");
}
}
// 长方形的高
double getHeight() {
return Math.abs(this.lefttop.getX() - this.rightbottom.getX());
}
// 长方形的宽
double getWidth() {
return Math.abs(this.lefttop.getY() - this.rightbottom.getY());
}
/**
* 判断是否是长方形 非长方形条件:x1=x2 或者是 y1=y2
*
* @return 是/否
*/
boolean isRect() {
if (this.lefttop.getX() == this.rightbottom.getX() || this.lefttop.getY() == this.rightbottom.getY()) {
return false;
} else {
return true;
}
}
}
- Circle.java
public class CirCle extends Graphic {
Point center;
int r;
public CirCle() {
this.color = "无色";
this.center = new Point();
this.r = 0;
}
public CirCle(String color,Point center, int r) {
this.color = color;
this.center = center;
this.r = r;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
@Override
double getArea() {
return Math.PI * r * r;
}
@Override
double getCircum() {
return 2 * Math.PI * r;
}
@Override
void printInfo() {
System.out.println("颜色:"+this.color + " 圆心:" + "("+this.center.getX()+","+this.center.getY()+")"+" 半径:"+ this.r +" 周长:"+getCircum()+" 面积:"+getArea());
}
/**
* 设置圆的参数信息
*
* @param color 颜色
* @param center 圆心
* @param r 半径
*/
void setinfo(String color, Point center, int r) {
this.color = color;
this.center = center;
this.r = r;
}
/**
* 改变圆的大小
*
* @param num 放大或缩小比例
*/
void changeSize(int num) {
this.r = r * num;
}
}
- Test.java
/**
* 测试类
*
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
//无参构造测试
Rect rect1 = new Rect();
rect1.setColor("红色");
rect1.setLefttop(new Point(1,1));
rect1.setRightbottom(new Point(3,4));
rect1.printInfo();
//Rect(String color, Point lefttop, Point rightbottom)测试
Rect rect2 = new Rect("白色",new Point(1,1),new Point(2, 2));
rect2.printInfo();
//Rect(String color, int x_left, int y_top, int x_right, int y_bottom)测试
Rect rect3 = new Rect("黄色",2,2,4,4);
rect3.printInfo();
Rect rect4 = new Rect("绿色",2,2,2,9);
rect4.printInfo();
//Circle 测试
CirCle cirCle1 = new CirCle();
cirCle1.printInfo();
CirCle cirCle2 = new CirCle("红色",new Point(1,1),3);
cirCle2.printInfo();
cirCle2.changeSize(2);
cirCle2.printInfo();
}
}
运行结果展示
本节完!
更多信息交流请加QQ:1406073270