2016-03-28 59 views
0

我有一个名为shape的接口,只用一个方法来计算形状的面积。然后我有两个类Shape和Rectangle来实现形状界面。所有的类都有相应的setter,getters和calcArea方法。主,我有一个ArrayList,将存储2个矩形和两个圆圈。我将如何将矩形和圆圈添加到数组中?我试图做shapeArr.,我的方法从矩形和圆不出现。我的代码如下,任何帮助表示赞赏。谢谢!两个类的ArrayList

public interface Shape { 
    public double calcArea(); 
} 


public class Circle implements Shape { 
    private int radius; 
    private double area; 

    public Circle() { 
     radius = 1; 
    } 

    public void setRadius(int r){ 
     radius = r; 
    } 

    public int getRadius(){ 
     return radius; 
    } 

    @Override 
    public double calcArea() { 
     area = (Math.PI * (Math.pow(radius, 2))); 
     return area; 
    } 
} 


public class Rectangle implements Shape{ 
    private int height; 
    private int width; 
    public int area; 

    public Rectangle(){ 
     height = 1; 
     width = 1; 
    } 

    public void setHeight(int h){ 
     height = h; 
    } 

    public void setWidth(int w){ 
     width = w; 
    } 

    public int getHeight(){ 
     return height; 
    } 

    public int getWidth(){ 
     return width; 
    } 

    @Override 
    public double calcArea() { 
     area = (height * width); 
     return area; 
    } 
} 

import java.util.ArrayList; 

public class ShapeDemo { 

    public static void main(String[] args) { 

     ArrayList<Shape> shapeArr = new ArrayList<Shape>(4); 

    } 

    public static void displayArea(Shape s) { 
     System.out.println("The area of the shape is " + s.calcArea()); 
    } 
} 
+0

但问题是什么?在你的数组对象中只能使用'Shape'类型的方法。如果你想使用特定的子类方法,你需要将对象从对象转换为特定类型,但在这种情况下,你应该完全知道什么是对象类型。 – sphinks

回答

2

你似乎与试图找到从Shape类型属于List的方法来挣扎。但是,您应该在List中寻找一种方法,该方法可以将Shape添加到集合中。你正在寻找的方法是List.add()

public static void main(String[] args) { 
    ArrayList<Shape> shapeArr = new ArrayList<Shape>(4); 
    Circle c = new Circle(); 
    c.setRadius(10); 
    Rectangle r = new Rectangle(); 
    r.setHeight(10); 
    r.setWidth(5); 

    shapeArr.add(c); 
    shapeArr.add(r); 
} 

现在,如果要计算每个Shape的面积在你的List,你只需要遍历集合并调用calcArea()方法:

for (Shape s : shapeArr) { 
    System.out.printf("Found a shape with area %.2f", s.calcArea()); 
} 
+0

谢谢,以前我做过这种方法。但是我被卡在印刷的地区。我忘了每个循环。谢谢。 – Blastoize34

+1

我建议使用'printf'而不是'println(... String.format' –

1

您需要将项目从ArrayList转换回正确的类型才能获得额外的字段。例如

(Circle)shareArr.get(1)

您可以使用instanceof运算符来找出它是什么类型。

for(Shape shape: shapeArr) 
{ 

    if(shape instanceof Circle) 
    { 
    (Circle)shape.circleMethod(); 
    } 
    //same for Rectangle 
    } 
} 
+1

或者您需要制作Shape中的所有方法。例如圆有一个宽度/高度 –

+0

@PeterLawrey矩形没有半径。 – kevingreen

+1

它有一个半宽。 ;) –

0

由于这两个类都实现了相同的接口,因此只需通过调用add方法将这些对象添加到ArrayList就没有问题。在您可以调用ArrayList中所有元素的displayArea方法来检查其区域之后。

public static void main(String[] args) { 
    ArrayList<Shape> shapeArr = new ArrayList<Shape>(4); 
    shapeArr.add(new Circle()); 
    shapeArr.add(new Rectangle()); 
    for (Shape s : shapeArr) { 
     displayArea(s); 
    } 
} 
+0

这种方法唯一的问题是如何设置宽度和高度或半径在主 – Blastoize34

+0

是的,我只是试图最小化代码最大限度地回答你的问题如果你想要你初始化两个对象,使用setters来设置高度/宽度(或者你可以声明另一个构造函数的高度和宽度),然后将这些对象传递给add方法数组列表。 – Mayday