由于Interface IArea中的参数Map
,我输出结果时遇到了麻烦public class Lambda {
public static void main(String[] args) {
double x=10,y=8,pi=3.14159265359,radius=7;
IArea triangle = (Map<ShapeProperties, Double> props) -> {
props.put(ShapeProperties.base, x);
props.put(ShapeProperties.height, y);
return (props.get(ShapeProperties.base) *
props.get(ShapeProperties.height) / 2);
};
IArea rectangle = (Map<ShapeProperties, Double> props) -> {
props.put(ShapeProperties.width, x);
props.put(ShapeProperties.length, y);
return (props.get(ShapeProperties.base) *
props.get(ShapeProperties.height));
};
IArea circle = (Map<ShapeProperties, Double> props) -> {
props.put(ShapeProperties.radius, radius);
return (pi*Math.pow(props.get(ShapeProperties.radius),2));
};
//this is the part where the error occurs.
System.out.println(triangle.getArea(Map<ShapeProperties, Double>
props));
}
}
有人可以指导我如何处理本练习的输出部分?提前谢谢。
答案 0 :(得分:0)
这是我的解决方案:
System.out.println(triangle.getArea(new HashMap<>()));
关键是您需要将Map对象传递给triangle.getArea()