读书人

大家来看看一下到底什么有关问题说

发布时间: 2011-12-04 22:58:58 作者: rapoo

大家来看看一下,到底什么问题,说说吧
import java.util.*;

abstract class Figure
{
protected String shape;

protected Figure(String shape)
{
this.shape = shape;
}

protected Figure()
{
this("未知");
}

public abstract double area();

public abstract double perimeter();

public void print()
{
System.out.println("一个" + this.shape + "," + this.toString() + ",周长为"
+ this.perimeter() + ",面积为" + this.area());
}
}

class Triangle extends Figure
{
protected double a;
protected double b;
protected double c;

public Triangle(double a, double b, double c)
{
super("三角形");
this.a = a;
this.b = b;
this.c = c;
}

public Triangle()
{
this(0, 0, 0);
}

public String toString()
{
return "a边长" + this.a + ",b边长" + this.b + ",c边长" + this.c;
}

public double area()
{
return (Math.sqrt((a + b + c) / 2.0) * (((a + b + c) / 2.0) - a)
* (((a + b + c) / 2.0) - b) * (((a + b + c) / 2.0) - c));
}

public double perimeter()
{
return (a + b + c);
}
}

到这里是正确的,编译通过,但是确实主函数
不知道以下抽象类的对象引用子类实例是否正确



class ClosedFigure_ex
{
public static void main(String args[])
{ClosedFigure g=new Triangle(3,4,5);
g.print();
}
}


[解决办法]

探讨

我想请问的是这个程序如何在JDK中运行啊,交一些方法。

读书人网 >J2SE开发

热点推荐