当类被声明为包私有时,我们应该声明一个公共构造函数吗? [英] Should we declare a public constructor when the class is declared as package private?

查看:179
本文介绍了当类被声明为包私有时,我们应该声明一个公共构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为在这种情况下,没有必要声明一个公共构造函数,因为类是不可访问的包外。但是当类只有包私有构造函数时,是否有一些隐藏的影响?

I think in this case there is no need to declare a public constructor since the class is not accessible outside the package anyway. But is there some hidden impact when the class has only package private constructor?

推荐答案

不,你不必声明公共构造函数;包私有构造函数将一样可用。类以外的包将无法使用构造函数,无论如何,因为他们看不到类。

No, you don't have to declare the public constructor; package private constructors will be just as usable. Classes outside the package wouldn't be able to use the constructor anyway, since they can't see the class.

这篇关于当类被声明为包私有时,我们应该声明一个公共构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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