想问一下为什么必需有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();
}
}
搜索更多相关的解决方案:
return
----------------解决方案--------------------------------------------------------
我实验了一下,如果合并成一行是不行的@!我想问一下是为什么啊???
----------------解决方案--------------------------------------------------------
因为你声明了有返回值,那就必须得有return 这个语句
----------------解决方案--------------------------------------------------------
你的方法在定义的时候就是有返回类型的 ,所以要有返回值.
----------------解决方案--------------------------------------------------------
哦,先谢谢上面两位!可是我在方法前面加上了void 还是不行啊!郁闷中!
----------------解决方案--------------------------------------------------------
抽象父类中缺少无参构造函数
----------------解决方案--------------------------------------------------------
abstract double area();
需要返回一个double类型的数据
----------------解决方案--------------------------------------------------------
谁能给我改一下,改成System.out.println("正方形的面积:"+length *width );
是在是郁闷!
----------------解决方案--------------------------------------------------------
要改的话抽象方法也要改成void型,三角形也要改,因为实现抽象方法的方法必须与抽象方法具有同样的签名
abstract class Shape
{
Shape()
{
}
protected double length;
protected double width;
Shape(double num,double num1)
{
length=num;
width=num1;
}
abstract void area();
public void disp()
{
System.out.println("我是在父类中产生的!");
}
}
class Square extends Shape
{
Square()
{
}
Square(double num, double num1)
{
super(num,num1);
}
void area()
{
System.out.println("正方形的面积为:"+length * width);
}
}
class Triangle extends Shape
{
Triangle(double num,double num1)
{
super(num,num1);
}
void area()
{
System.out.println("三角形的面积为:"+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;
fobj.area();
fobj=trobj;
fobj.area();
Shape a = new Square();
a.disp();
}
}
----------------解决方案--------------------------------------------------------
哈哈哈,徒弟~
----------------解决方案--------------------------------------------------------