Java:当涉及父/子时,如何在ArrayList中获取值? [英] Java: How to I get values inside ArrayList when there's parent/child involved?

查看:42
本文介绍了Java:当涉及父/子时,如何在ArrayList中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个对象的ArrayList.这些对象内部有各种属性及其值.

So I have an ArrayList of objects. Inside those objects are various attributes and their values.

代码非常简单.GBox和GCircle是GHP的子代.ArrayList在世界上.

The code is pretty simple. GBox and GCircle are childs of GHP. The ArrayList is in World.

我要打印的是HP和盒子的体积以及HP和圆的直径.我知道我可以覆盖 toString(),但是我实际上想获取这些值.这样做的正确语法是什么?

What I want to do is print the HP and volume of the box and the HP and diameter of the circle. I understand I could override toString() but I actually want to get the values. What's the correct syntax to do so?

//Main.java
public class Main {
    public static void main(String[] args) {
        Ini i = new Ini();
    }
}

//Ini.java
public class Ini {
    private static World w;

    public Ini() {
        w = new World;
        w.makeGBox();
        w.makeGCircle();
        System.out.println("Box: HP: " +
                           w.getList().get(0).getHP() +
                           "Volume: " +
                           w.getList().get(0).GBox.getVolume());
                           //compile error no variable GBox in GHP
        System.out.println("Circle: HP: " +
                           w.getList().get(1).getHP() +
                           "Radius: " +
                           w.getList().get(1).GCircle.getRadius());
                           //compile error no variable GCircle in GHP
    }
}

//World.java
import java.util.ArrayList;

public class World {
    private ArrayList<GHP> list = new ArrayList<>();

    public void makeGBox() {
        list.add(new GBox());
    }
    public void makeGCircle() {
        list.add(new GCircle());
    }

    public ArrayList<GHP> getList() {
        return list;
    }
}

//GHP.java
public class GHP {
    private int HP;

    public GHP() {
        setHP(5);
    }

    public int getHP() {
        return HP;
    }
    public void setHP(int HP) {
        this.HP = HP;
    }
}

//GBox.java
public class GBox extends GHP{
    private int volume;

    public GBox() {
        setVolume(10);
    }

    public int getVolume() {
        return volume;
    }
    public void setVolume(int volume) {
        this.volume = volume;
    }
}

//GCircle.java
public class GCircle extends GHP{
    private int radius;

    public GCircle {
        setRadius(7);
    }

    public int getRadius() {
        return radius;
    }
    public void setRadius(int radius) {
        this.radius = radius;
    }
}

推荐答案

除了许多编译问题之外,您还需要进行这些更改才能实现所需的功能.

Apart from the many compilation problems, you need these changes to achieve what you want.

for (GHP ghp : w.getList()) { // Avoid using get(index) without a forloop, as such
    if (ghp instanceof GBox) { // Using the instanceof operator, you can differentiate the 2 class types
        System.out.println("Box: HP: " + ghp.getHP() + "Volume: "
                + ((GBox) ghp).getVolume()); // Cast it to GBox to be able to call getVolume
    }

    if (ghp instanceof GCircle) {
        System.out.println("Circle: HP: " + ghp.getHP() + "Radius: "
                + ((GCircle) ghp).getRadius());// Cast it to GCircle to be able to call getRadius 
    }
}

这篇关于Java:当涉及父/子时,如何在ArrayList中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆