先说明,我代码有问题,不是偷懒。
这个是我有错误的代码,我不理解的就是怎么把Square和Circle的getArea用进去啊。
interface Shape{
public double getArea();
}
class Square implements Shape{
double sideLen;
Square(double sideLen){
this.sideLen=sideLen;
}
public double getArea(){
return sideLen*sideLen;
}
}
class Circle implements Shape{
int radius;
public double getArea(){
return Math.PI*radius*radius;
}
}
public class TestGeneric<Shape>{
Shape shape;
TestGeneric(Shape shape){
this.shape=shape;
}
public double calcArea(){
return shape.getArea();
}
public static void main(String[]args){
Square sq=new Square(3.3);
TestGeneric<Square> a=new TestGeneric<Square>(sq);
a.calcArea();
}
} |