5D艺术网首页
商城
|
资讯
|
作品
|
博客
|
教程
|
论坛
登录
注册
加为好友
发短消息
来自:
性别:秘密
最后登录:2007-04-12
http://anlong.5d.cn/
首页
|
新闻
|
话题
|
博客
|
相册
|
艺术作品
|
社交关系
|
留言板
|
社交圈
2005/10/22 | 抽象类
类别(ProGrammE)
|
评论
(0)
|
阅读(61)
|
发表于 22:29
首先定义一个点,然后利用点的特性,
定义两个点决定的一条线,然后就是一个圆,
老师叫我们再加一个矩形,怎么加啊? -_-"
package abstracttest;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
abstract class Graphic
{
public static final double PI=3.14;
double area(){
return 0 ;
}
double perimeter(){
return 0;
}
abstract void draw();
}
class Point extends Graphic
{
protected double x,y;
public Point(double x,double y)
{
this.x=x;
this.y=y;
}
void draw(){
System.out.println("Draw a point at ("+x+","+y+")");
}
public String toString()
{
return "("+x+","+y+")";
}
}
class Line extends Graphic{
protected Point p1,p2;
public Line(Point p1,Point p2)
{
this.p1=p1;
this.p2=p2;
}
public double perimeter()
{
double temp=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
return Math.sqrt(temp);
}
void draw()
{
System.out.println("Draw a line from"+p1+"to"+p2);
}
}
class Circle extends Graphic{
protected Point o;
protected double r;
public Circle(Point o,double r)
{
this.o=o;
this.r=r;
}
double area(){return r*r*PI;}
void draw(){
System.out.println("Draw a circle at"+o+"and r is"+r);
}
double perimeter(){return 2*PI*r;}
}
class Graphic1 extends Graphic {
protected Point w1,w2,h1,h2;
public double width,height;
public Graphic1(Point w1,Point w2,Point h1,Point h2) {
this.w1=w1;
this.w2=w2;
this.h1=h1;
this.h2=h2;
double tempw=(w2.x-w1.x)*(w2.x-w1.x)+(w2.y-w1.y)*(w2.y-w1.y);
double tempd=(h2.x-h1.x)*(h2.x-h1.x)+(h2.y-h1.y)*(h2.y-h1.y);
width=Math.sqrt(tempw);
height=Math.sqrt(tempd);
}
double area() {
return width*height;
}
void draw() {
System.out.println("Draw a Graphic1 at "+w1+w2+h1+h2+" and width is "+width+",height is "+height);
}
double perimeter() {
return 2*(width+height);
}
}
public class GraphicDemo1{
public static void main(String [] args){
Graphic[] g=new Graphic[4];
g[0]=new Point(10,10);
g[1]=new Line(new Point(10,10),new Point(20,30));
g[2]=new Circle(new Point(10,10),4);
g[3]=new Graphic1(new Point(0,0),new Point(5,0),new Point(0,5),new Point(5,5));
for(int i=0;i<g.length;i++)
{
g[i].draw();
System.out.println("Area="+g[i].area());
System.out.println("Perimeter="+g[i].perimeter());
}
}
}
0
评论
Comments
日志分类
首页
[161]
AbouT
[22]
Article
[25]
ScHOoL
[20]
PCGamE
[2]
AmusEMent
[85]
ProGrammE
[7]