Java - 内部类构造函数 - 仅允许外部类 [英] Java - Inner class constructor - allowed for outer class only

查看:478
本文介绍了Java - 内部类构造函数 - 仅允许外部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中有内部类。我想给它的实例的公共访问,但只有外部类应该能够创建这个实例,如在私有访问。

I have inner class in my code. I want to give public access to its instances, but only outer class should be able to create this instances, like in "private" access. Is it possible without making properly small package (or creating public interface for every such inner class)?

(对不起,如果我的英语不好:P)

(Sorry if my english is bad :P)

推荐答案

这是可能的。声明您的内部类public,但其构造函数private 。这种方式你可以创建它只在你的封闭类和本身,而不是从外部。

It is possible. Declare your inner class public, but its constructor private. This way you can create it only inside your enclosing class and itself, but not from outside.

这篇关于Java - 内部类构造函数 - 仅允许外部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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