关于泛型的问题,我是新手,不大理解及使用,请大虾们不吝赐教!!
程序代码:import java.util.*;
interface I1{
float getArea();
}
class Triangle implements I1
{
float a;
float b;
float c;
String s = "Triangle";
Triangle (float a,float b,float c){
this.a = a;
this.b = b;
this.c = c;
}
float getArea(){
float p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c ));
}
}
class Rectangle implements I1
{
float width;
float length;
String s = "Rectangle";
float getArea(){
return width * length;
}
};
class Square implements I1
{
float a;
String s = "Square";
float getArea(){
return a * a;
}
};
class Circle implements I1
{
float r;
String s = "Circle";
final float PI = 3.1415926f;
float getArea(){
return PI*r*r;
}
};
public void printArea(T){
System.out.println("The area of this" + T.s + ":" + T.getArea() );
}
public class GraphicsTest
{
public static void main(String[] args){
List<int>strList = new ArrayList<int>();
strList.add(t.getArea());
strList.add(r.getArea());
strList.add(s.getArea());
strList.add(c.getArea());
Triangle t = new Triangle (3.3f,6.6f,5.5f);
Rectangle r = new Retangle(5.6f,3.8f);
printArea(r);
Square s = new Square(2.9f);
printArea(s);
Circle c = new Circle(4.8f);
printArea(c);
}
};
输出的错误结果:E:\09008129>javac GraphicsTest.java
GraphicsTest.java:57: 需要为 class、interface 或 enum
public void printArea(T){
^
GraphicsTest.java:62: 需要为 class、interface 或 enum
}
^
2 错误
//这是什么意思啊!我这道题要求用泛型输出各种图形的面积,可是我似乎不大理解泛型,估计是泛型的那个方法写错了,应该怎么修改呢?为什么?还希望各位大虾帮忙解答!!小弟在这里谢过了!!








