Is it possible to have a private class?

前端 未结 4 1251
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 13:40

I always wonder if it is possible to have a private class? And, what would be the point of having such class?

Thanks for helping.

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 14:02

    Having private non-nested classes (Visible only to their namespace and child namespaces only) would allow to clean code boundaries while programming in the same assembly.

    Having for example only an interface and a factory visible from other namespaces in the same assembly while still having all the implementation of the interface and utility classes (that no-one have business knowing out of the namespace) there.

    It is still possible to do it somewhat with a big partial class replacing a namespace and nested classes inside but it's a very bad hack and unit testing become nearly impossible.

提交回复
热议问题