是否可以开设私人班级? [英] Is it possible to have a private class?

查看:55
本文介绍了是否可以开设私人班级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是想知道是否可以开设私人课程?而且,上这样的课有什么意义呢?

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

感谢您的帮助.

推荐答案

是的,可以有一个私有类,但只能作为另一个类的内部类:

Yes it is possible to have a private class, but only as an inner class of another class:

public class Outer
{
    private class Inner
    {}
}

当您想将某个逻辑封装在一个类(外部的)中,但是需要更结构化/OO设计的代码来实现它时,这通常很有用.过去,当我需要一个容器类来处理某个类的方法中的某些信息时,我就使用了这种模式,但是容器类在此逻辑之外没有任何意义.将容器类设为私有内部类意味着将其使用本地化为使用它的外部类.

This is usually useful when you want to encapsulate some logic inside of a class (the outer one), but need a more structured/OO design of code to implement it. I have used this pattern in the past when I need a container class to process some information within a method of a class, but the container class has no meaning outside of this logic. Making the container class a private inner class means that its use is localised to the outer class that utilises it.

值得注意的是,通过这种结构,内部类可以访问外部类的私有成员,但反之则不行.

It is worth noting that with this structure, the inner class has access to the private members of the outer class, but not the other way around.

这篇关于是否可以开设私人班级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆