Java - 如何从ArrayList打印某个对象的方法?

时间:2014-11-18 01:32:26

标签: java arraylist

基本上我有一个对象的ArrayList。有不同类型的对象,例如“三角形”,“矩形”和“球体”。每个对象都有自己的方法,例如getArea,getPerimeter和getVolume。这些方法对于每个对象是不同的。如何遍历ArrayList以打印出每个对象的所有方法?

所以说我从一个三角形开始。如何使用for循环和switch block / if语句打印其getArea和getPerimeter方法?

4 个答案:

答案 0 :(得分:1)

您可以使用简单的for循环迭代ArrayList,然后调用对象的toString()方法:

for (int = 0; i < arrayList.size(); i++)
{
    System.out.println(arrayList.get(i).toString());
}

要覆盖类的toString方法,只需添加方法,非常简单:

class Rectangle
{
    //Member variables
    int width, height; //Example

    //Member functions
    getArea { ... }

    //toString
    public String toString()
    {
        return String.valueOf(getArea());
    }
}

答案 1 :(得分:0)

for(int i=0; i<listOfObjects.length; i++) {
   Object obj = listOfObjects[i];

   if (obj instanceof Triangle) {
      Triangle t = (Triangle) obj;
      t.getArea();
   } else if (obj instanceof Rectangle) {
      Rectangle r = (Rectangle) obj;
      r.getPerimeter();
   } else {
      Sphere s = (Sphere) obj;
      s.getSomeOtherThing();
   }
}

答案 2 :(得分:0)

检查一下。 instanceof运算符的替代

    ArrayList arr=new ArrayList();
    arr.add(new Traingle());
    arr.add(new Rectangle());
    arr.add(new Sphere());
    for(Object obj:arr){
        int value=0;
        switch (obj.getClass().getSimpleName()){
        case "Rectangle":
            value=((Rectangle) obj).getPerimeter();
            break;
        case "Traingle":
            value=((Traingle) obj).getArea();
            break;
        case "Sphere":
            value=((Sphere) obj).getVolume();
            break;
        default:
            break;
        }
        System.out.println("Object "+obj.getClass().getSimpleName()+":"+value);
    }

答案 3 :(得分:0)

为什么不使用界面?

package com.spiderman;

public interface Shape {
    public void getArea();

    public void getPerimeter();

    public void getVolume();
}

package com.spiderman;

public class Rectangle implements Shape {
    @Override
    public void getArea() {
        System.out.println("area of Rectangle");
    }
    @Override
    public void getPerimeter() {
        System.out.println("perimiter of Rectangle");   
    }
    @Override
    public void getVolume() {
        System.out.println("volume of Rectangle");
    }
}

package com.spiderman;

public class Triangle implements Shape {
    @Override
    public void getArea() {
        System.out.println("area of Triangle"); 
    }
    @Override
    public void getPerimeter() {
        System.out.println("perimiter of Triangle");    
    }
    @Override
    public void getVolume() {
        System.out.println("volume of Triangle");   
    }
}

package com.prash;

public class Sphere implements Shape {
    @Override
    public void getArea() {
        System.out.println("area of Sphere");
    }
    @Override
    public void getPerimeter() {
        System.out.println("perimiter of Sphere");
    }
    @Override
    public void getVolume() {
        System.out.println("volume of Sphere");
    }
}


package com.spiderman;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Shape> list = new ArrayList<Shape>();

        list.add(new Rectangle());
        list.add(new Triangle());
        list.add(new Sphere());

        for (int i = 0; i < list.size(); i++) {
            Shape shape = list.get(i);
            shape.getArea();
            shape.getPerimeter();
            shape.getVolume();
            System.out.println("------------------");
        }
    }
}

输出:

area of Rectangle
perimiter of Rectangle
volume of Rectangle

area of Triangle
perimiter of Triangle
volume of Triangle

area of Sphere
perimiter of Sphere
volume of Sphere