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();
}
}
------解决方案--------------------