2015-12-02 34 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