开发者

can't access inner class's public variable... which is inherited from parent

Java Q: I can't access a public variable in my parent's class' inner class called foo. Why? setup is next(pseudo coded for brevity):

public class PageObject  
{  
    public class Button  
    {  
        public String foo ="I want this string."  //ca开发者_如何学JAVAn't access....  
    }  
    ....other stuff I can access here...  
}  

public class worker
    {  
    public PageObject p = new PageObject();  
    }  

public class workerchild extends worker  
{  
    p.Buttons.    <---don't have access to Buttons public variables, only .class, etc.  
}  


p.Button is a classname.
Like any other classname, it can only be used to access static members.

You need to get an instance of the Button class. (eg, p.new Button().foo)


First off, your inner class is called Button (singular), not Buttons (plural). Second, make the inner class static and the foo member a constant and you will be able to access the foo member as simply Button.foo, you just won't be able to change its value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜