dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [DotGNU]How do you simulate an inner class in C#?


From: Thong \(Tum\) Nguyen
Subject: RE: [DotGNU]How do you simulate an inner class in C#?
Date: Mon, 26 May 2003 01:57:28 +1200

> -----Original Message-----
> From: Mohan Embar [mailto:address@hidden
> Sent: Monday, 26 May 2003 1:26 a.m.
> To: address@hidden; Gopal V
> Subject: Re: [DotGNU]How do you simulate an inner class in C#?
> 
> Hi Gopal,
> 
> >Like, I'm thinking about having an extra arg for each inner class
> >constructor (just like ^Tum suggested you to emulate) and pull the
> >"this" reference from wherever it's called and push into "parent" ?.
> 
> At first glance, this seems right, although you will have to support
some
> other constructs too, like the outer this pointer.
> 
> >From everything I see, inner classes (actually, we're talking
non-static
> member classes, since inner classes also include anonymous and local
> classes) are the same as nested classes except they have the outer
> class' this pointer.
> 
> Expanding ^Tum's example, you can also do this:
> 
> public class Outer
> {
>    int x;
> 
>    public final String toString()
>    {
>        return "Woof!";
>    }
> 
>    public class Inner
>    {
>        public final String toString()
>        {
>            return "Meow!";
>        }
> 
>        public void Foo()
>        {
>            x = 10;
>        }
> 
>        public void stuff()
>        {
>            System.out.println(this);       // Meow!
>            System.out.println(Outer.this); // Woof!
>            System.out.println(x);          // 10
>        }
>    }
> }
> 
> >So the inner classes are inaccessible from outside ? (unlike nested
> >classes ?)
> 
> They can be accessible from outside (public class Inner) - I think
they're
> like nested classes with the outer this pointer. ^Tum?
> 

Hi there :)

AFAIK inner classes are like normal classes except with the name
mangling.
Private inner classes are private to that class (similar to un-nested
top level private classes in the same .java file).  Other inner classes
are theoretically public but the compiler ensures that they can't be
constructed from outside the outer class (or specialized versions of the
outer class).

^Tum



reply via email to

[Prev in Thread] Current Thread [Next in Thread]