簡體   English   中英

找不到符號符號:方法位置:類

[英]cannot find symbol symbol: method location: class

對不起,只是學習Java。 但是,有人可以告訴我為什么我遇到“找不到符號”錯誤嗎? 我的代碼如下:

public class NumberHolder {
  public int anInt;
  public float aFloat;

  public NumberHolder(int setAnInt, float setAFloat) {
    setAnInt = anInt;
    setAFloat = aFloat;
  }

  public static void main(String[] args) {
    NumberHolder newNumber = NumberHolder(12, 24F);
  }
}

看起來您在調用構造函數之前缺少了一個new函數:

NumberHolder newNumber = new NumberHolder(12, 24F);

編輯:而且,正如塔索斯·巴蘇科科斯(Tassos Bassoukos)在他的回答中指出的那樣,您需要在構造函數中轉換工作:

anInt = setAnInt;
aFloat = setAFloat;

盡管個人而言,我還是喜歡這樣寫我的構造函數:

public NumberHolder(int anInt, float aFloat) {
  this.anInt = anInt;
  this.aFloat = aFloat;
}

但是,這是樣式和個人喜好問題。

以來

public NumberHolder(int anInt, float aFloat);

是構造函數,而不是普通方法,您需要使用關鍵字new才能獲取實際對象。 您像一個方法一樣調用它,並且沒有名為NumberHolder的任何方法(但是如果有的話,它將是有效的)

除了缺少的new關鍵字之外,構造函數中的賦值也應該相反。

您需要使用new關鍵字實例化新對象。

public class NumberHolder {
    public int anInt;
    public float aFloat;

    public NumberHolder(int anInt, float aFloat) {
        this.anInt = anInt;
        this.aFloat = aFloat;

    }

    public static void main(String[] args) {
        NumberHolder newNumber = new NumberHolder(12, 24F);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM