gpt4 book ai didi

java - 尝试从另一个类访问字符串?

转载 作者:行者123 更新时间:2023-12-01 10:48:15 24 4
gpt4 key购买 nike

我无法从 Picture 类访问 String 描述,以便在 PhotoViewer 类中使用它。我创建了一个 getter 方法,所以我不明白为什么会收到错误

cannot find symbol - method 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

最佳答案

这就是解决方案。我使用了类型转换。

enter image description here

关于java - 尝试从另一个类访问字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34053461/

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