|
本帖最後由 MemoriseOff 於 2012-2-19 22:00 編輯
小孟老師有說用方法寫出這題目,下面是可以編譯的,但是我把System那行改了一下,編譯就出錯了,請問問題是出在哪邊?
可以編譯的:
class CShape
{
public double area()
{
return 1.0;
}
}
class CCircle extends CShape
{
double radius;
public double area(double r)
{
return r*r*3.1415926;
}
}
class CSquare extends CShape
{
double side;
public double area(double s)
{
return s*s;
}
}
class CTriangle extends CShape
{
double base,height;
public double area(double b,double h)
{
return b*h/2;
}
}
public class test
{
public static void main(String args[])
{
CShape s01=new CCircle();
System.out.println(((CCircle)s01).area(2.0));
CShape s02=new CSquare();
System.out.println(((CSquare)s02).area(2.0));
CShape s03=new CTriangle();
System.out.println(((CTriangle)s03).area(2.0,2.0));
}
}
編譯錯誤的
public class test
{
public static void main(String args[])
{
CShape s01=new CCircle();
System.out.println(s01.area(2.0));
CShape s02=new CSquare();
System.out.println(s02.area(2.0));
CShape s03=new CTriangle();
System.out.println(s03.area(2.0,2.0));
}
}
test.java:37: error: method area in class CShape cannot be applied to given type
s;
System.out.println(s01.area(2.0));
^
required: no arguments
found: double
reason: actual and formal argument lists differ in length
test.java:40: error: method area in class CShape cannot be applied to given type
s;
System.out.println(s02.area(2.0));
^
required: no arguments
found: double
reason: actual and formal argument lists differ in length
test.java:43: error: method area in class CShape cannot be applied to given type
s;
System.out.println(s03.area(2.0,2.0));
^
required: no arguments
found: double,double
reason: actual and formal argument lists differ in length
3 errors |
|