抽象类

http://blog.5d.cn/user3/anlong/200510/174838.html

 

首先定义一个点,然后利用点的特性,
定义两个点决定的一条线,然后就是一个圆,
老师叫我们再加一个矩形,怎么加啊? -_-"
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());
}
}

}
TAGS:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值