2013-08-21 93 views
-2

這是我的第一個Java項目。無法對靜態對象進行靜態引用

所以我正在開發自己的模擬項目,而我的一些核心內容已經出錯了。我現在有兩門課我正在關注 - 定居點和townRey,它延伸瞭解決辦法。

將引發錯誤,當我嘗試

System.out.println(townRey.returnStrength()); 

這裏是我的兩個相關類:

結算:

public class settlement 
{ 
    // 
    // 
    // VARIABLES 
    // 
    // 

    /** 
    * The town's unique name. 
    */ 
    public String name; 

    /** 
    * The settlement's location in latitude (N-S) 
    */ 
    public int latitude; 

    /** 
    * The settlement's location in longitude (E-W) 
    */ 
    public int longitude; 

    /** 
    * What faction a town or village is aligned to. This determines production and consumption, mostly. 
    */ 
    public String faction; 

    /** 
    * What a specific village or town produces. 
    */ 
    public String[] production; 

    /** 
    * What a specific town consumes (villages don't consume) 
    */ 
    public String[] consumption; 

    /** 
    * How dangerous a specific town is with bandits 
    * A 1-10 scale, with 10 being the most dangerous. 
    * Any town with a danger over 8 can be raided and destroyed temporarily by bandits. 
    * Being raided successfully depends on the Strength of a town. 
    */ 
    public int danger; 

    /** 
    * How much a town takes in taxes. 
    */ 
    public float tax; 

    /** 
    * How easily a town is raided by bandits. 
    * If a bandit raid has a lower strength than the town, then the town wins. 
    */ 
    public int strength; 

    // 
    // 
    // METHODS 
    // 
    // 

    public int returnLatitude() 
    { 
     return latitude; 
    } 

    public int returnLongitude() 
    { 
     return longitude; 
    } 

    public String returnFaction() 
    { 
     return faction; 
    } 

    public String[] returnProduction() 
    { 
     return production; 
    } 

    public String[] returnConsumption() 
    { 
     return consumption; 
    } 

    public int returnDanger() 
    { 
     return danger; 
    } 

    public float returnTax() 
    { 
     return tax; 
    } 

    public int returnStrength() 
    { 
     return strength; 
    } 
} 

和townRey:

public class townRey extends settlement 
{{ 
    name = "Rey"; 
    latitude = 5; 
    longitude = 5; 
    String faction = "Nord"; 
    String[] production; 
    String[] consumption; 
    danger = 1; 
    tax = 0.05F; 
    strength = 6; 
}} 

編輯: : 感謝 所有的幫助!我現在解決了所有問題。以下是「解決」和「開始」。

public class Settlement 
{ 
    // 
    // 
    // VARIABLES 
    // 
    // 

    /** 
    * The town's unique name. 
    */ 
    public String name; 

    /** 
    * The settlement's location in latitude (N-S) 
    */ 
    public int latitude; 

    /** 
    * The settlement's location in longitude (E-W) 
    */ 
    public int longitude; 

    /** 
    * What faction a town or village is aligned to. This determines production and consumption, mostly. 
    */ 
    public String faction; 

    /** 
    * What a specific village or town produces. 
    */ 
    public String[] production; 

    /** 
    * What a specific town consumes (villages don't consume) 
    */ 
    public String[] consumption; 

    /** 
    * How dangerous a specific town is with bandits 
    * A 1-10 scale, with 10 being the most dangerous. 
    * Any town with a danger over 8 can be raided and destroyed temporarily by bandits. 
    * Being raided successfully depends on the Strength of a town. 
    */ 
    public int danger; 

    /** 
    * How much a town takes in taxes. 
    */ 
    public float tax; 

    /** 
    * How easily a town is raided by bandits. 
    * If a bandit raid has a lower strength than the town, then the town wins. 
    */ 
    public int strength; 

    // 
    // 
    // METHODS 
    // 
    // 

    public int returnLatitude() 
    { 
     return latitude; 
    } 

    public int returnLongitude() 
    { 
     return longitude; 
    } 

    public String returnFaction() 
    { 
     return faction; 
    } 

    public String[] returnProduction() 
    { 
     return production; 
    } 

    public String[] returnConsumption() 
    { 
     return consumption; 
    } 

    public int returnDanger() 
    { 
     return danger; 
    } 

    public float returnTax() 
    { 
     return tax; 
    } 

    public int returnStrength() 
    { 
     return strength; 
    } 
} 

和開始,在那裏我創建'townRey'然後以兩種不同的方式訪問一些數據。

public class Start 
{ 
    public static void main(String[] args) 
    { 
     //Creates 'Rey' 
     Settlement townRey = new Settlement(); 
     townRey.name = "Rey"; 
     townRey.latitude = 5; 
     townRey.longitude = 5; 
     townRey.faction = "Nord"; 
     townRey.danger = 1; 
     townRey.tax = 0.05F; 
     townRey.strength = 6; 

     //This calls the returnLongitude method from Settlement, and is the 'proper' way to do it. 
     System.out.println(townRey.returnLongitude()); 

     //This also works. 
     System.out.println(townRey.longitude); 

     //Thanks for the help! 
    } 
} 
+6

*「這是我的第一個Java項目。」* - 請不要用小寫字母來啓動Java類名。永遠不會!現在學習本課。 –

+0

雖然我經常聽到這種說法,並且隨之而來,因爲它是流行的風格,所以我很想知道爲什麼這個話題激發了人們之間的這麼多的憤怒。例如,以小寫和大寫變量開頭的標準有什麼問題? – qaphla

+1

@qaphla如果這是標準的話,那就沒有什麼問題了。這個標準是任意的,但普遍存在,使用錯誤的大寫字母就像拼寫錯誤:讀者通常可以弄清楚你的意思,但是花費更多的精力來解決實際問題。 – chrylis

回答

0

townRey不應該延期結算。你應該聲明爲解決一些方法的一個實例,如下所示:

townRey = new settlement(); 
townRey.name = "Rey"; 
... 
townRey.strength = 6; 

或者更好的是,使結算,是以不同的領域作爲輸入一個新的構造。

另外,樣式註釋:一般來說,在Java中,類應該以大寫字母開頭,所以結算而不是結算可能會成爲一個更好的名稱。

0

你應該定義則townRey對象使用這個對象調用returnStrength

townRey mytownRey = new townRey(); 
System.out.println(townRey.returnStrength()); 
0

我希望你要townRey是的settlement一個實例,而不是一個子類。除非你想擁有townRey的多個副本。將行public class townRey extends settlement替換爲settlement townRey = new settlement(),並在}}後面加上分號。讓其他所有事情都一樣。

0
public class mainclss() 
{ 
public static void main(String arg[]) 
{ 
townRey= new settlement(); 
//you can do sth you like 
} 
} 

創建一個新類來檢查。不要使用Class來啓動Java!它有點困難。

0

main()方法創建一個單獨的類。在這個方法內部,你應該創建一個townRey對象,以便訪問方法returnStrength()。如果您這樣做,則無法使用類名「townRay」訪問它。所以添加這個類下面的代碼:

public class MainClass { 

    public static void main(String[] args) { 

     townRey tr = new townRey(); 
     System.out.println(tr.returnStrength()); 


    } 

} 

工作我沒意見。所以你可以放心地使用它。

注:你應該實踐中學習,開始每個單詞在類名稱以大寫字母,如SettlementTownRey。祝你好運!

相關問題