无法访问内部类的公共变量...它是从父类继承的
Java 问:我无法访问父类的内部类 foo 中的公共变量。为什么?接下来是设置(为简洁起见,使用伪编码):
public class PageObject
{
public class Button
{
public String foo ="I want this string." //can'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.
}
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." //can'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.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
p.Button
是一个类名。与任何其他类名一样,它只能用于访问静态成员。
您需要获取
Button
类的实例。 (例如,p.new Button().foo
)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
)首先,您的内部类称为 Button(单数),而不是 Buttons(复数)。其次,将内部类设置为静态并将
foo
成员设置为常量,您将能够以Button.foo
的形式访问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 thefoo
member as simplyButton.foo
, you just won't be able to change its value.