2017-08-24 59 views
-2

我有一個主類遊戲名爲打磚塊的訪問Veriables從另一個類

public class BrickBreaker extends Activity { 
    // there is lot of other code but i am only pointing to the issue 
    class BreakoutView extends SurfaceView implements Runnable { 
    // The size of the screen in pixels 
    int screenX; 
    int screenY; 

    // Get a Display object to access screen details 
     Display display = getWindowManager().getDefaultDisplay(); 
     // Load the resolution into a Point object 
     Point size = new Point(); 
     display.getSize(size); 

     screenX = size.x; 
     screenY = size.y; 
}} 

而且從另一個類(下)我想從主類訪問screenX

public class Paddle { 

    // This the the constructor method 
// When we create an object from this class we will pass 
// in the screen width and height 
public Paddle(int screenX, int screenY){ 
    // 130 pixels wide and 20 pixels high 
    length = 130; 
    height = 20; 

    // Start paddle in roughly the sceen centre 
    x = screenX/2; 
    y = screenY - 20; 

    rect = new RectF(x, y, x + length, y + height); 

    // How fast is the paddle in pixels per second 
    paddleSpeed = 550; 

} 
    public void update(long fps){ 
    rect.left = x; 
     rect.right = x + length; 
     if (x<0){ 
     x=0; 
     } 
     else if (x+length > screenX){ 
     x = screenX-length; 
     } 
    } 
} 

哪有我從Paddle類訪問screenX

+2

通過使'screenX'訪問。可以通過公開,也可以通過爲其提供吸氣劑,並且通過給「BrickPad」對象引用「Paddle」。 – cello

+0

我完全不擅長Java,請告訴我如何使用你解釋的兩種方法使它可訪問。請@cello – Andriopak

回答

1

在槳類修改現場screenX

Java靜態變量

如果聲明任何變量作爲靜態的,它被稱爲靜態變量。

靜態變量可以被用於指代所有對象的共同屬性(即不爲每個對象是唯一的)例如員工的公司名稱,學生的學院名稱等。 靜態變量在類加載時只在班級區域獲得一次內存。 優勢靜態變量的

請檢查:STATIC

它使你的程序的內存使用效率(即它可以節省內存)。

public class Paddle { 

public static int screenX; 

    // This the the constructor method 
// When we create an object from this class we will pass 
// in the screen width and height 
public Paddle(int screenX, int screenY){ 
this.screenX = screenX; 
    // 130 pixels wide and 20 pixels high 
    length = 130; 
    height = 20; 

    // Start paddle in roughly the sceen centre 
    x = screenX/2; 
    y = screenY - 20; 

    rect = new RectF(x, y, x + length, y + height); 

    // How fast is the paddle in pixels per second 
    paddleSpeed = 550; 

} 
    public void update(long fps){ 
    rect.left = x; 
     rect.right = x + length; 
     if (x<0){ 
     x=0; 
     } 
     else if (x+length > screenX){ 
     x = screenX-length; 
     } 
    } 
} 

並以BrickBeaker存取權限,你需要:

Paddle.screenX 

享受

+0

我不會爲此推薦一個靜態變量。 – cello

+0

你是我的老闆,你接近我的心理層面,你解釋了我能夠輕鬆理解的任何事情。還有一件事需要知道。如果我不聲明它是靜態的,它對代碼的影響是什麼?我的意思是我無法從BrickBreaker或任何其他問題訪問它? @羅德 – Andriopak

+0

查看更新的答案。是的,你不能在課堂上使用非靜態字段。靜態你可以做到這一點。 – Rodriquez

1

您需要從Paddle類可以訪問BreakoutView的成員screenX

對於這一點,你的槳類首先需要獲得訪問BreakoutView實例。

您可以通過它傳遞例如在構造函數中做到這一點:

public class Paddle { 
    BreakoutView view; 
    public Paddle (BreakoutView view) { 
    this.view = view; 
    } 

    public void update(long fps){ 
    ... 
    } 
} 

你在哪裏創建它:

BreakoutView view = new BreakoutView(.....); 
Paddle paddle = new Paddle(view); 
.... 

接下來,你需要獲得訪問screenX成員。有兩個選項:

第一:讓市民:

有了這個解決方案,您可以用this.view.screenX訪問它在你的Paddle類。

但是,這將允許其他代碼,例如從你的槳類中,修改這些值,這往往是不理想。因此,更安全的方法是:

第二:提供一個吸氣劑。

在BreakoutView,添加下面的方法:

public int getScreenX() { 
    return this.screenX; 
} 

,然後在你的Paddle類,你可以像this.view.getScreenX(),而不是僅僅screenX訪問它。

+0

我已經在paddle類中有一個構造方法,我無法創建另一個。現在我想使用screenX進入更新方法,但我不知道我該怎麼做@cello請幫助,我已經編輯了上面的代碼與槳的構造函數。 – Andriopak

+0

將「int screenX」成員添加到Paddle類中,並在Paddle構造函數中將其賦值爲'this.screenX = screenX'。然後你可以在更新方法中使用screenX。 – cello