黑马程序员技术交流社区

标题: 分享年龄的格式 [打印本页]

作者: 邱峁    时间: 2012-12-10 16:41
标题: 分享年龄的格式

class MyShape
{
private double m_hou;

public MyShape()
{
m_hou = 0.1;
}

public MyShape(double x)
{
m_hou = x;
}

public double getTiJi()
{
return getMianJi() * m_hou;
}

public double getMianJi()
{
return 0;
}
}

class Circle extends MyShape
{
private double m_r;

public Circle(double r, double hou)
{
super(hou);
m_r = r;
}

public Circle(double r)
{
m_r = r;
}

public double getMianJi()
{
return Math.PI * m_r * m_r;
}
}

class Rect extends MyShape
{
private double m_a; // 长
private double m_b; // 宽

public Rect(double a, double b)
{
m_a = a;
m_b = b;
}

public Rect(double a)
{
m_a = a;
m_b = a;
}

public double getMianJi()
{
return m_a * m_b;
}
}


class Triangle extends MyShape
{
private double m_a; // 三边长
private double m_b; //
private double m_c; //

public Triangle(double a, double b, double c, double hou)
{
super(hou);
m_a = a;
m_b = b;
m_c = c;
}

public double getMianJi()
{
double s = (m_a+m_b+m_c)*0.5;
return Math.sqrt(s*(s-m_a)*(s-m_b)*(s-m_c));
}
}


public class MuXing
{
public static void main(String[] args)
{
MyShape[] x = new MyShape[5];

x[0] = new Circle(2.5, 0.5); // 半径,厚度
x[1] = new Circle(2.5);
x[2] = new Rect(2.5,1.5); // 长,宽
x[3] = new Triangle(2,3,4,0.1); // 三边,厚度
x[4] = new Rect(4); // 方形

double sum = 0;
for(int i=0; i<x.length; i++){
sum += x[i].getTiJi();
}

System.out.println(sum);
}
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2