English 中文(简体)
c 仅可由上级即时的公共阶层
原标题:c# public class that can only be instantiated by its parent
  • 时间:2012-05-16 13:57:39
  •  标签:
  • c#
  • oop

Is it possible to have a nested class that is public but can only be instantiated by it s parent class e.g.

 public class parent
{
    public class child
    {
        public string someValue;
    }

    public child getChild()
    {
        return new child();
    }
}

举例来说,儿童班级可以通过父母以外的法典进行。 我希望外部法典能够看到儿童类型,但无法自行制定。

e.g

var someChild = new parent.child();
最佳回答
  1. Make a public interface.
  2. Make the child class private.
  3. Make the child implement the interface.
  4. Have the getChild method make a new child and return the interface type.

如评论和其他答复所述,你还可以将内层建筑商的准入权改变为内部或私人的,同时让内层本身公开。

问题回答

界定内部空构者

public class child     
{         
    internal child() {}
    public string someValue;     
}  

您可将创作限制在您的构造(儿童班级)的保护水平,以达到<条码>。

@Servy所讲述的或接口。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签