题目要求:综合作业目的
利用面向对象的思想解决问题
掌握类、封装、继承(抽象类)、多态(接口)、组合(对象成员)的基本使用方法了解GUI、异常处理内容(如下述内容或自选)
(1)抽象图形类Geo,属性: string color颜色:
属性Integer. x Integer y, Integer z表示图形坐标,其中二维图形的z=null抽象方法:平移图形的两个方法public abstract void transform (Point p),其中的参数Point为自定义的类型Point类(选做派生子类Piont2D和Point3D)和public abstract void transform (Integer x Integer Y. Integer z),
重写该方法的子类中对于二维图形,修改x和y坐标,对于三维图形修改x, y和z坐标:(2)子类:圆形circle,球体Sphere派生自Geo, 并重写并实现transform方法。(3)接口Draw,抽象方法透视投影的方法, public abstract project();
Sphere类实现该接口并重写接口方法,此方法只要输出类和方法按名称即可(4) DrawingGeo类,属性: Geo [或ArrayList
方法: public void display()方法能够输出当前类中的所有图形数据(5)测试类
(6) GUI和异常处理选做
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
/*DrawGeo app = new DrawGeo();
app.addGeo(new Circle(10,10));
app.addGeo(new Sphere("Red",20,20,20));
app.display();
Sphere sp=new Sphere();
//sp.project();*/
Point p=new Point(2,3,5);
Circle c=new Circle(2,3);
c.transform(p);
String s=c.toString();
System.out.println(s);
}
}
class DrawGeo{
ArrayList geoList = new ArrayList(); //将每个geo对象封装进数组里面作为一个数组对象
public void display() { //使用增强for循环输出所有属性,Geo类重写了tostring方法,可以直接输出属性
for(Geo o: geoList)
System.out.println(o);
}
public void addGeo(Geo o) { //用了构造方法直接创建对象的同时赋值
geoList.add(o);
}
}
abstract class Geo{
String color = "Black";
Integer x;
Integer y;
Integer z;
public Geo() {
x =0; y = 0;
}
public Geo( Integer x, Integer y, Integer z) { //不带颜色的构造方法
this.x = x; this.y = y; this.z = z;
}
public Geo(String color, Integer x, Integer y, Integer z) { //带颜色的构造方法
this.color = color; this.x = x; this.y = y; this.z = z;
}
public abstract void transform(Point p);
public abstract void transoform(Integer x, Integer y, Integer z);
}
class Circle extends Geo{
public Circle() { //继承父类的空参构造
super();
}
public Circle(Integer x, Integer y) { //继承父类有参构造,将z值赋为空
super(x, y,null);
}
public Circle(String color,Integer x, Integer y) {
super(color, x, y,null);
}
@Override
public void transform(Point p) { //移动方法使圆的坐标加上点的坐标即为移动
x=x+p.a;
y=y+p.b;
}
@Override
public void transoform(Integer x, Integer y, Integer z) {
this.x = x; this.y=y;
}
public String toString() { //重写tostring方法可用于输出
return "Circle[color="+super.color+", (x,y)=("+x+", "+ y+")]";
}
}
class Sphere extends Geo implements Draw{
public Sphere() {
super();
}
public Sphere(Integer x, Integer y, Integer z) {
super(x, y,z);
}
public Sphere(String color,Integer x, Integer y, Integer z) {
super(color, x, y,z);
}
@Override
public void project() { //定义的扫描方法只为了输出Shpere: project()
System.out.println("Shpere: project()");
}
@Override
public void transform(Point p) {
x=x+p.a;
y=y+p.b;
z=z+p.c;
}
@Override
public void transoform(Integer x, Integer y, Integer z) {
this.x = x; this.y = y; this.z = z;
}
public String toString() {
return "Shpere[color="+super.color+", (x,y,z)=("+x+", "+ z+", "+ z+")]";
}
}
interface Draw{
public void project();
}
class Point{ //定义了点对象,三个参数的构造方法,可以直接创建对象的同时赋值
Integer a;
Integer b;
Integer c;
public Point(Integer a,Integer b,Integer c) {
this.a=a;this.b=b;this.c=c;
}
}