gpt4 book ai didi

java - 使用 ArrayList Java 中的方法

转载 作者:行者123 更新时间:2023-12-02 03:40:27 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com