2015-12-02 69 views
0

我無法從我的Picture類訪問String說明,以便在PhotoViewer類中使用它。我創建了一個getter方法,所以我不明白爲什麼我得到錯誤試圖從另一個類訪問字符串?

無法找到符號 - 方法getDescription()。

下面是代碼:

/** 
* Write a description of class Picture here. 
* 
* @author (your name) 
* @version (a version number or a date) 
*/ 
public class Picture extends Attachment 
{ 
    protected String description; 
    protected String height; 
    protected String width; 

    /** 
    * Constructor for objects of class Picture 
    */ 
    public Picture(String filename, long size, String description, String height, String width) 
    { 
     this.filename = filename; 
     this.size = size; 
     this.description = description; 
     this.height = height; 
     this.width = width; 
    } 

    /** 
    * Prints out the details of a picture attachment. 
    */ 
    public void preview() 
    { 
     System.out.println("Filename: " + filename); 
     System.out.println("Size: " + size); 
     System.out.println("Description: " + description); 
     System.out.println("Height: " + height); 
     System.out.println("Width: " + width); 
    } 

    /** 
    * Getter method that returns the description 
    */ 
    public String getDescription() 
    { 
     return description; 
    } 
} 

/** 
* Application to open and view photos 
* 
* @author Chandler Warren 
* @version 12-1-15 
*/ 
public class PhotoViewer extends Application 
{ 
    Picture picture; 

    /** 
    * Constructor for objects of class PhotoViewer 
    */ 
    public PhotoViewer() 
    { 

    } 

    /** 
    * Abstract method to open the attachment 
    */ 
    public void open(Attachment picture) 
    { 
     System.out.println("I am the PhotoViewer. You are viewing a picture of " + getDescription()); 
    } 
} 

New error encountered when trying to initiate picture

+1

應該是'picture.getDescription()'? – JCOC611

+2

而方法參數應該是圖片,而不是附件。無論如何,這個附件類是什麼? –

+0

@HovercraftFullOfEels其實,它應該是Attachment。我跟我的老師檢查過。如果有幫助,我會添加附件類。 –

回答

0
public void open(Attachment picture) 
{ 
    System.out.println("I am the PhotoViewer. You are viewing a picture of " + picture.getDescription()); 
} 

將工作假設picture正確instatiated(目前尚不清楚,如果你想使用實例變量或th e方法變量,這個例子調用方法變量,但this.picture會調用實例變量)。 getDescription()是一個實例方法,因此必須從一個對象中調用。

+0

'this'隱式地是一個Object,問題是'this'沒有實現'getDescription()' – WalterM

+0

@WalterM方法我的關於'this'的原因是OP可能通過嘗試訪問實例變量上的方法'picture'而不是方法'picture'的參數。我從來不會調用'this.getDescription()',所以我不確定問題是什麼。 –

+0

我該如何正確安裝它? –

1

這是解決方案。我用鑄造。

enter image description here

+0

雖然這個鏈接可能回答這個問題,最好包括基本部分並提供鏈接以供參考。如果鏈接頁面更改,僅鏈接答案可能會失效 – Marusyk