2012-05-03 51 views
-2

我有一個名爲Shape的類,它有兩個子類Shape1和Shape2。 Shape類我有變數XPOS和XPOS和方法,即:安裝程序的難度

public int getXpos(){ 
    return Xpos; 
} 

public void setXpos(int x){ 
    this.x = x; 
} 

// Same thing for y 

現在,讓我們只說在類形,x = 10。現在,當我繼承它:

public class Shape1{ 
    Shape1(){ 
     xPos = 100; 
     // ... 
    } 
} 

和:

public class Shape2{ 
    Shape2(){ 
     xPos = 200; 
     // ... 
    } 
} 

但是當我做Shape1.getX()在另一個程序中,我得到10結果。有人能告訴我爲什麼我沒有100? 「this」關鍵字存在問題嗎?

+1

您的代碼不完整,與示例不一致。請解決你的問題。 – BalusC

+1

這個問題是各種形式的困惑。 –

+0

你可以發佈你的實際代碼嗎,你可能是在引用錯誤的對象。 –

回答

0

XPOS不能是靜態的。如果它是靜態的,相同的數字將出現兩次。 (原始形狀)

3

getXpos()方法應該是這樣的:

public int getXpos() {  
    return x; 
}