2021-06-04
发布日期:2022-03-08 21:50:46
浏览次数:4
分类:技术文章
本文共 6281 字,大约阅读时间需要 20 分钟。
深克隆的小例子-创建画布
题目描述
现有一块画布上(Canvas)它有如下功能:
定义addShape(Shape s)在画布上新增并绘制出其形状; 定义removeShape(Shape s)删除画布上已存在的形状 定义clone(Shape s)克隆一个已存在的形状增加到画布上 定义Shape Max()方法返回画布上面积最大的形状(如果有相等的几个返回第一个) 定义public double getArea()方法求画布上所有形状的面积和 定义printAllShapes()方法打印画布上所有形状信息 每一个形状(Shape)类,都有创建时的日期数据域(createdDate),创建的颜色(color),求面积和绘制图形的方法等等,每一个形状都是能被比较和复制(复制的时候,形状的时间应该是被复制的时间而不是被复制的形状的创建时间)。长方形和圆形是形状类的子类,它们会重写父类求面积和绘制图形的方法。为了程序实现方便,我们这里只定义两种图形。圆形(circle)和长方形(rectangle)。 uml图如下:简要分析
画布和形状是聚合的关系,画布类应该定义add,remove,print等方法。shape类应该为抽象类,他的子类circle和rectangle要实现Comparable和Cloneable接口,实现Comparable接口是为啦方便对象数组排序,需要重写ComparTo()方法;实现Cloneable接口是为啦方便clone对象,需要重写Clone()方法。
Canvas类
去页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
Canvans类:package Canvans;import java.util.Collections;import java.util.Comparator;import java.util.ArrayList;public class canvas { private ArrayListshapes = new ArrayList<>(); private double Area; public void addShape(Shape shape) { shapes.add(shape); shape.draw(); } public void removeShape(Shape shape) { shape.del(); shapes.remove(shape); } public double getArea() { for (Shape shape : shapes) { Area += shape.getArea(); } return Area; } public Shape ShowMax() { if(shapes == null) { return null; }// Collections.sort(shapes,new compare()); Collections.sort(shapes); return shapes.get(0); } public void printAllShapes() { for(Shape shape:shapes) { System.out.println(shape.toString()); } }}//注释部分代码是之前编写的指定比较器,可忽略//class compare implements Comparator { //// public int compare(Shape a,Shape b)// { // if(a.getArea() b.getArea())// { // return 1;// }// return 0;// }//}
Shape类
public abstract class Shape implements Comparable,Cloneable{ private java.util.Date whenBuilt; private String color; public abstract double getArea(); public abstract void draw(); public abstract void del(); public abstract int compareTo(Shape shape);}
Circle类
import java.util.Date;import static java.lang.Math.PI;public class Circle extends Shape implements Comparable,Cloneable{ private double radius; private String color; private java.util.Date whenBuilt; Circle(String color,double radius) { this.radius = radius; this.color = color; this.whenBuilt = new Date(); } public double getRadius() { return radius; } public String getColor() { return color; } public java.util.Date getWhenBuilt() { return whenBuilt; } public void setRadius(double radius) { this.radius = radius; } public void setColor( String color) { this.color = color; } public void setWhenBuilt(Date whenBuilt) { this.whenBuilt = whenBuilt; } @Override public double getArea() { return radius*radius*PI; } @Override public void draw() { System.out.println("正在画一个圆形...Circle [ radius = "+radius+"getCreatedDate() = "+whenBuilt+", getColor()= "+color); } public void del() { System.out.println("正在删除一个圆形...Circle [ radius = "+radius+"getCreatedDate() = "+whenBuilt+", getColor()= "+color); } @Override public int compareTo(Shape shape) { if(shape.getArea()>this.getArea()) { return 1;} else if(shape.getArea()
Rectangle类
import java.util.Date;public class Rectangle extends Shape implements Comparable,Cloneable{ private java.util.Date whenBuilt; private double width; private double length; private String color; Rectangle(String color,double width,double length) { this.color = color; this.length = length; this.width = width; this.whenBuilt = new Date(); } public double getWidth() { return width; } public double getLength() { return length; } public String getColor() { return color; } public java.util.Date getWhenBuilt() { return whenBuilt; } public void setWidth(double width) { this.width = width; } public void setLength(double length) { this.length = length; } public void setColor( String color) { this.color = color; } public void setWhenBuilt(Date whenBuilt) { this.whenBuilt = whenBuilt; } @Override public double getArea() { return width*length; } @Override public void draw() { System.out.println("正在画一个方形...Rectangle [ width = "+width+", length = "+length+" getCreatedDate() = "+whenBuilt+", getColor()= "+color); } public void del() { System.out.println("正在删除一个方形...Rectangle [ width = "+width+", length = "+length+" getCreatedDate() = "+whenBuilt+", getColor()= "+color); } @Override public int compareTo(Shape shape) { if(shape.getArea()>this.getArea()) { return 1;} else if(shape.getArea()
##test类
package Canvans;public class testmain { public static void main(String[] args) throws CloneNotSupportedException{ //创建c1圆的对象 Circle c1 = new Circle("yellow",1.0); //创建c2圆的对象 Circle c2 = new Circle("white",2.0); //创建r1矩形的对象 Rectangle r1 = new Rectangle("pick", 3, 4); //创建r2矩形的对象 Rectangle r2 = new Rectangle("black", 2, 5); //创建c画布的对象 canvas c = new canvas(); //在画布上加入并绘制c1,c2,r1和r2 c.addShape(c1); c.addShape(c2); c.addShape(r1); c.addShape(r2); System.out.println(); //在画布上删除c1对象 c.removeShape(c1); c.printAllShapes(); System.out.println(); //复制r1对象赋值给r3并加入在画布中 Rectangle r3 =(Rectangle)(r1.clone()); c.addShape(r3); //修改r1对象的属性 r1.setColor("green"); r1.setLength(20); r1.setWidth(10); // 注意r3和r1输出结果的不同,长,宽和日期 c.printAllShapes(); System.out.println(); //求所有形状中面积最大的并打印其信息 Shape p = c.ShowMax(); System.out.println(p.toString()); }}
运行截图
转载地址:https://blog.csdn.net/m0_55677778/article/details/117571949 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月15日 00时59分01秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
VTK:应用之有限元分析
2019-04-26
VTK:应用之算法可视化
2019-04-26
VTK:片段Snippets
2019-04-26
VTK:AnimateActors动画演员
2019-04-26
VTK:旋转球RotatingSphere
2019-04-26
VTK:多行文字MultiLineText
2019-04-26
VTK:文字来源TextOrigin
2019-04-26
VTK:XYPlot图
2019-04-26
VTK:检查模块CheckForModule
2019-04-26
VTK:复合数据之CompositePolyDataMapper
2019-04-26
VTK:复合数据之Generate2DAMRDataSetWithPulse
2019-04-26
VTK:复合数据之Generate3DAMRDataSetWithPulse
2019-04-26
VTK:复合数据之MultiBlockDataSet
2019-04-26
VTK:复合数据之OverlappingAMR
2019-04-26
VTK:数据结构之BuildLocatorFromKClosestPoints
2021-06-29
VTK:数据结构之BuildOctree
2021-06-29
VTK:数据结构之ClosestNPoints
2021-06-29
IDEA SpringMVC Tomcat 404 一网打尽 及路径问题汇总【踩坑记录】
2021-06-29
VTK:几何对象之PolyLine
2021-06-29