2016-04-27 42 views
0

我正嘗試使用我在Array列表中的類中的方法。使用ArrayList中的方法Java

ArrayList是ArrayList,裝置是超類。

ArrayList包含擴展Appliance(如時鐘和燈)的對象。

當我使用

arrayList.get(x)的抓取在該點的對象我看不到的方法,所述對象具有,我只能看到超類對象。

請有人可以幫助我。

感謝您閱讀

代碼(有些)

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 
+0

你能展示一些示例代碼嗎? – Austin

回答

2

我假設你正在做這樣的事情(抱歉,但目前尚不清楚,從你的問題):

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

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

問題是,java不知道你在那個位置的元素是一個SuperClass,裝置還是其他從SuperClass繼承的東西。你可以投的代碼,使其行爲您想要的方式:

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

您可能還需要使用instanceOf運營商,以確保你有你期待的類的實例。

+1

有人抱怨我們的答案已經降低。 – DavidS

+0

謝謝我使用((Clock)myApp)強化了它.getHours – LittleFallenOne

+1

他們總是在做@DavidS。 –

0

這聽起來像你寫

List<Appliance> appliances = new ArrayList<>(); 
appliances.add(new Lamp()); 
appliances.add(new Clock()); 

Appliance appliance = appliances.get(0); 
appliance.setAlarm(TOMORROW); 

我覺得從這個例子可以看到,爲什麼你不能訪問子類的方法。當您擁有設備列表時,您不知道其中的對象是時鐘還是燈,因此當您拿出一個設備時,無法調用子類方法。

如果你是積極的對象是一個時鐘,你可以將它轉換:

Clock clock = (Clock) appliances.get(1); 
clock.setAlarm(TOMORROW); 

但這不是Java的方式。通常,您只能在設備上使用超類方法,或爲時鐘和燈具維護一個單獨的列表。

+0

我只是這樣做,因爲它是我的簡介告訴我做的方式 – LittleFallenOne

0
 ArrayList<Appliance> mylist = new ArrayList(); 
    for(Appliance app : mylist){ 

    boolean isAclock = app instanceof Clock; 
    boolean isALamp = app instanceof Lame; 

     if(isAlamp){ 
     Lamp l = (Lamp)app; 
     }else{ 
     Clock c = (Clock)app; 
     } 

    } 

讓你的arralist接受appliacnces將接受它的子類bt問題是接受正確的類對象。您可以使用實例並檢查對象類型,然後將對象轉換爲其原始類型 嘗試使用此方法

0

這就是預期的行爲。這就是多態性是如何工作的。

讓我們來看看使用自己的類的例子。想象一下拿着一個包。你知道只有Appliance物品可以放進這個包裏,所以你可以把你家裏的所有電器放進這個包裏 - 你的Lamp,你的Clock,也許你的ToasterBlender,以及其他一些物品。

現在想象你穿上眼罩並隨機抽出其中一個電器。不看它,你怎麼知道它是什麼設備?你不能!如果你認爲你剛剛拿出的東西是Toaster,並且你試圖用toast()你的麪包,如果你實際持有Blender會發生什麼?可能你會得到一個混亂的混亂。因此,所有對象都可以做的唯一事情是turnOn()turnOff(),因此這些是唯一可用的方法。

在這個例子中,你是編譯器,包是列表。如果您告訴編譯器該列表只能包含Appliance對象,除非您明確告訴它爲(例如,如果您通過執行某些操作將Appliance對象向下轉換爲Toaster),它將不會假設列表中的對象有任何其他內容如((Toaster)applianceList.get(0)).toast())。