想问一下为什么必需有return
abstract class Shape{
protected double length;
protected double width;
Shape(double num,double num1)
{
length=num;
width=num1;
}
abstract double area();
public void disp()
{
System.out.println("我是在父类中产生的!");
}
}
class Square extends Shape
{
Square()
{
}
Square(double num, double num1)
{
super(num,num1);
}
double area()
{
System.out.println("正方形的面积为:"); //可以这样写吗System.out.println("正方形的面积:"+length *width );
return(length * width);
}
}
class Triangle extends Shape
{
Triangle(double num,double num1)
{
super(num,num1);
}
double area()
{
System.out.println("三角形的面积为:");
return((0.5*length*width));
}
}
public class CalculateArea
{
protected CalculateArea()
{
}
public static void main(String [] args)
{
Shape fobj;
Square sqobj = new Square(10,20);
Triangle trobj = new Triangle(12,8);
fobj=sqobj;
System.out.println(fobj.area());
fobj=trobj;
System.out.println(fobj.area());
Shape a = new Square();
a.disp();
}
}