我了解到静态嵌套类应该像外部类的字段一样被访问(第2行)。但即使直接实例化内部类也有效(第 1 行)。你能帮我理解一下吗?
public class OuterClass
{
public OuterClass()
{
Report rp = new Report(); // line 1
OuterClass.Report rp1 = new OuterClass.Report(); // line 2
}
protected static class Report()
{
public Report(){}
}
}
最佳答案
accessed like a field of outer class
这就是你正在做的事情。想象一下:
class OuterClass
{
SomeType somefield;
static SomeType staticField;
public OuterClass()
{
//works just fine.
somefield = new SomeType();
//also works. I recommend using this
this.somefield = new SomeType();
//the same goes for static members
//the "OuterClass." in this case serves the same purpose as "this." only in a static context
staticField = new SomeType();
OuterClass.staticField = new SomeType()
}
}
关于java - Java中访问静态嵌套类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870460/