2013-02-25 55 views
0

我想知道是否有人可以讓我知道如何從我在主(小程序)類中創建的對象從我的主(小程序)類訪問對象。來源可能會澄清一些事情。通常我會用存取但這是爲了簡單起見從內部類訪問父級小程序

public class Bravo { 

    int copyint; 

    Bravo() { 

    // Here I want to access the targetobj's theint from here 
    copyint = targetobj.theint; // I belive this doesn't work 
    } 
} 

public class Charlie { 

    static int theint; 

    Charlie() { 

    theint = 7; 
    } 
} 

public class alpha extends JApplet { 

    public void init() { 

    createApp(); 
    } 

    public void createApp() { 

    Charlie targetobj = new Charlie(); 
    Bravo askingobj = new Bravo(); 
    } 
} 

TYIA -Roland

+0

_I相信這不工作_當你嘗試編譯時發生了什麼? – 2013-02-25 03:40:31

+0

這是我的頭頂,但我真的懷疑這將在它的現狀 – 2013-02-25 03:41:29

+0

你可以看到我的答案在下面,這應該工作。 – 2013-02-25 03:43:00

回答

0

static int theint你已經宣佈它作爲一個靜態的。所以你不需要使用object來訪問這個變量。你可以只用className.variable,像這樣:

Charlie.theint; 

這會工作,提供類是其它類可見。

+0

好吧,我的例子錯了我會重新發布一些能夠更準確地反映我的問題的東西......除非它那麼簡單。嗯tyvm,但我將不得不重新發布 – 2013-02-25 03:44:39