Instantiating inner class

I think you want to declare the HashPerson class as static. Otherwise it can only be instantiated in the context of the containing class, either in a method of the containing class or using code like this: ContainingClass container = new ContainingClass(); HashPerson william = container.new HashPerson(“willy”); Actually, my rule-of-thumb is to make any nested … Read more

Nested classes’ scope?

class Outer(object): outer_var = 1 class Inner(object): @property def inner_var(self): return Outer.outer_var This isn’t quite the same as similar things work in other languages, and uses global lookup instead of scoping the access to outer_var. (If you change what object the name Outer is bound to, then this code will use that object the next … Read more

Why Would I Ever Need to Use C# Nested Classes [duplicate]

A pattern that I particularly like is to combine nested classes with the factory pattern: public abstract class BankAccount { private BankAccount() {} // prevent third-party subclassing. private sealed class SavingsAccount : BankAccount { … } private sealed class ChequingAccount : BankAccount { … } public static BankAccount MakeSavingAccount() { … } public static BankAccount … Read more

How to access outer class from an inner class?

You’re trying to access Outer’s class instance, from inner class instance. So just use factory-method to build Inner instance and pass Outer instance to it. class Outer(object): def createInner(self): return Outer.Inner(self) class Inner(object): def __init__(self, outer_instance): self.outer_instance = outer_instance self.outer_instance.somemethod() def inner_method(self): self.outer_instance.anothermethod()