java - 使用 ArrayList Java 中的方法

标签 java arraylist

我正在尝试使用数组列表中的类中的方法。

ArrayList 是 ArrayList,其中 Appliance 是父类(super class)。

ArrayList 包含扩展 Appliance 的对象,例如时钟和灯。

当我使用时

arrayList.get(x) 来获取对象,此时我看不到该对象具有的方法,我只能看到父类(super class)对象。

请有人帮助我。

感谢您的阅读

代码(部分)

public abstract class Appliance implements Serializable {

    protected boolean power;
    protected ImageIcon picture;

    public Appliance() {
    }

   public void setPower(boolean power) {
        this.power = power;
    }

    public boolean getPower() {
        return power;
    }

    abstract ImageIcon getPicture();

    @Override
    public String toString() {

        String powerVal;
        if (this.power == true) {
            powerVal = "ON";
        } else {
            powerVal = "OFF";
        }

        return "Power: " + powerVal;

    }

}



public class Clock extends Appliance {

    private int hours;
    private int minutes;
    private int seconds;

    public Clock() {
        super();
        this.power = false;
        this.picture = new ImageIcon("src/res/clock.jpg");
        this.hours = 23;
        this.minutes = 59;
        this.seconds = 59;
    }

    public Clock(boolean pow, int hours, int minutes, int seconds) {
        super();
        this.power = pow;
        this.picture = new ImageIcon("src/res/clock.jpg");
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }

    public int getHour() {
        return this.hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public int getMinutes() {
        return this.minutes;
    }

    public void setMinutes(int minutes) {
        this.minutes = minutes;
    }

    public int getSeconds() {
        return this.seconds;
    }

    public void setSeconds(int seconds) {
        this.seconds = seconds;
    }

    @Override
    public ImageIcon getPicture() {
        return this.picture;
    }

    @Override
    public String toString(){
       return super.toString() + String.format(" and the time is %d:%d:%d",this.hours, this.minutes, this.seconds);
    }

}

public class Lamp extends Appliance{


    //Default constructor or Empty argument constructor
    public Lamp(){
        super();
        this.power = false;
        this.picture = new ImageIcon("src/res/lamp.jpg");
    }
    public Lamp(boolean pow){
        super();
        this.power = pow;
        this.picture = new ImageIcon("src/res/lamp.jpg");
    }
    @Override
    ImageIcon getPicture() {       
        return picture;
    }    

    @Override
    public String toString(){
        return super.toString();
    }
}



public class Controller {

private ArrayList<Appliance> myAppliances = new ArrayList<>();
 private JLabel[] labelArray;

...................................................


    @Override
    public void mouseClicked(MouseEvent me) {

        String[] options = new String[]{"yes","no"};

        if (me.getButton() == 1){
          try{  

           int x = Integer.parseInt( me.getComponent().getName());

            Appliance myApp = this.myAppliances.get(x);

           if(myApp.getClass().equals(Clock.class)){

               JOptionPane.showOptionDialog(null, "Clock Info: /nTime: "  + myApp., "Clock", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options,options[1] );
           }

          } catch (Exception e){
              System.out.println("Null Poiter");
          }

        }

    }

}


Its the myApp. part in the clicked method

最佳答案

我假设你正在做这样的事情(抱歉,但你的问题并不清楚):

ArrayList<SuperClass> al;
...populated with instances of Appliance

al.get(1).getClock();  //Compile error.

问题是 java 不知道该位置的元素是 SuperClass、Appliance 还是从 SuperClass 继承的其他元素。您可以转换代码,使其按照您想要的方式运行:

((Appliance)al.get(1)).getClock();

您可能还想使用 instanceOf 运算符来确保您确实拥有所需类的实例。

关于java - 使用 ArrayList Java 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36895975/

相关文章:

java - 如何使用 Spring AOP 通知具有特定 id 的 X 类 bean 而不是 X 类的所有 bean

java - 从 LibreOffice Java SDK 获取页数

java - IRCLib Java 创建的 IRC Bot 问题

java - 如何添加通过解析获得的值以获得 "value"的 "column"?

java - JSON解析到ArrayList

java - 使用 ContentResolver 检索 SMS 的 NullPointerException 导致应用程序崩溃

java - hibernate 。如何将自己的类映射为实体中的属性类型?

java - JFrame标题栏重叠图形

java - 定义了 equals 和 hashCode,arrayList 方法仍然不起作用

java - Java中的可变长度(动态)数组