开发者

Scope of Nested Classes

If I have a nested class, does anything from开发者_如何学Python the owning class exist in the owned class?

for example:

public class OwningClass 
{

    int randomVariable = 1;

    public void MakingMethod()
    {
        OwnedClass owned = new OwnedClass();
        owned.SomeMethod();
    }

    private class OwnedClass
    {
        public void SomeMethod()
        {
            // Is anything from OwningClass available here?
        }

    }

}


Anything "static" from the owning class is available in your nested class.

If you have an instance of the owning class in some method of the inner class, you may also access its private members.


The only thing a nested class changes with respect to it's parent class is accessibility. The nested class can access private members of the containing type.


Not unless you passed in a reference of the OwningClass in the constuctor of the OwnedClass or through the SomeMethod of OwnedClass.

Enjoy!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜