[英]In Java, what is the actual class for a superclass variable that is referencing a subclass reference?
[英]Java Class subclass variable reference
我上課時一直遇到“找不到符號”錯誤。 該變量在超類中明確聲明,但子類看不到它。 除了在RecordViewer
子類中JLabel
的新構造函數上,我沒有收到任何錯誤。
class RecordViewer extends JDialog{
private JButton next;
private JButton prev;
private JLabel label;
private int current;
public RecordViewer(CDinventoryItem [] array){
super();
current = 0;
final CDinventoryItem [] items = array;
label = new JLabel(items[getCurrent()]);
來自我的CDinventoryItem類的預定義toString ...
@Override public String toString(){
// Decimal foramting for the inventory values
NumberFormat dformat = new DecimalFormat("#0.00");
// Formatting for the inventory output
StringBuilder ouput = new StringBuilder();
String New_Line = System.getProperty("line.separator");
ouput.append("The product number of my CD is: ").append(iPitemNumber).append (New_Line);
ouput.append("The title of the CD is: ").append(sPtitle).append (New_Line);
ouput.append("I have ").append(iPnumberofUnits).append(" units in stock.").append (New_Line);
ouput.append("The total value of my inventory on this product is: ").append(dformat.format(stockValue())).append (New_Line);
return ouput.toString();
}
那是標准的Java JLabel嗎? 您正在嘗試傳遞CDinventoryItem類型的對象,並且JLabel將沒有構造函數來處理這種參數,除非它擴展了String或Icon類。
JLabel
JLabel
沒有定義采用您的自定義類型的構造函數。 您需要在此處傳遞一個字符串。 只是一個猜測,但是您的問題用語暗示您試圖直接從子類中使用私有字段嗎? 除了在其中聲明了類的地方(包括相同繼承層次結構中的類)之外,在其他任何地方都看不到私有字段。 您可以使它受保護,但是這是令人不滿意的-最好提供一個mutator方法來標記或更好地在超類構造函數中對其進行初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.