# Shape.java
package shape_demo;
public abstract class Shape
{
public abstract double sumAreas();
}
# Rectangle.java
package shape_demo;
public class Rectangle extends Shape
{
@Override
public double sumAreas()
{
return 1;
}
}
# Triangle.java
package shape_demo;
public class Triangle extends Shape
{
@Override
public double sumAreas()
{
return 0.3;
}
}
# Shapes.java
package shape_demo;
public class Shapes
{
public static double sumAreas(Shape[] shapes)
{
double sum = 0;
for (Shape s : shapes)
{
sum += s.sumAreas();
}
return sum;
}
}
# Shape_Driver.java
package shape_demo;
public class Shape_Driver
{
public static void main(String[] args)
{
Shape[] shapes = {new Rectangle(), new Triangle()};
System.out.println(Shapes.sumAreas(shapes));
}
}
댓글 영역