包私有类中的公共方法 [英] public methods in package-private classes

查看:76
本文介绍了包私有类中的公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在程序包专用类中将方法标记为public是否有所不同?

Does it make a difference to mark methods as public in package-private classes?

class SomePackagePrivateClass
{
    void foo();          // package private method

    public void bar();   // public method
}

foobar之间的可见性是否有实际差异?

Is there any practical difference in visibility between foo and bar here?

推荐答案

如果该类不会被另一个更可见的子类*扩展,则唯一的区别是意图明确.将所有方法包都声明为私有,使以后的读者更难确定哪个方法将由同一包中的其他类调用.

If the class is not going to be extended by another, more visible subclass*, the only difference is clarity of intent. Declaring all methods package private makes it more difficult for future readers to determine which of the methods are meant to be called by other classes in the same package.

* 作为我的设计解决方案并没有多大意义,但是从技术上讲还是可以的.

这篇关于包私有类中的公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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