0

我了解到静态嵌套类应该像外部类的字段一样访问(第 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(){}
    }
}
4

1 回答 1

1

像外部类的字段一样访问

这就是你正在做的事情。想象一下:

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()
    }
}
于 2016-11-29T17:48:04.870 回答