首页 > 其他 > 详细

方法修饰符

时间:2019-08-05 13:18:23      阅读:75      评论:0      收藏:0      [点我收藏+]

Illegal modifier for the method name; only public, protected, private, abstract, static, final, synchronized, native & strictfp are permitted

方法名称的非法修饰符; 只允许使用public,protected,private,abstract,static,final,synchronized,native和strictfp

技术分享图片

 一、public

可跨包结构调用类对象的public方法

对于跨包调用,非public修饰的方法不可见。

The method sex() from the type PublicUtilTest is not visible

译:PublicUtilTest类型的方法sex()不可见

技术分享图片

 

技术分享图片

 

可以跨包调用public修饰的方法。

二、protected

不可继承其它包内的非public类

PublicUtilA cannot be resolved to a type

译:PublicUtilA无法解析为某种类型

技术分享图片

protected可以跨包继承

The method PublicA.description() does not override the inherited method from PublicUtilTest since it is private to a different package

译:PublicA.description() 方法不会覆盖PublicUtilTest中的继承方法,因为它是私有的来自不同的包

技术分享图片

 

同一个包内进行继承可继承包权限及以上的方法。

技术分享图片

 

三、private

若private修饰的私有方法,未被调用,会出现警告。不影响使用,但要尽量避免从未使用过的方法和变量出现,减少代码冗余。

The method speak() from the type PublicUtilTest is never used locally

译:PublicUtilTest类型的方法speak()从不在本地使用

技术分享图片

 

 private只能在本类其他方法中被调用,无法由对象实例直接调用。

通过Change visibility of ‘speak()‘ to ‘package‘后会变成无修饰符方法 void speak(),可实现对象实例调用,但仅限于本包内,包权限的方法没有修饰符如PublicUtilA.description()。

技术分享图片

 

四、

方法修饰符

原文:https://www.cnblogs.com/feiyang930112/p/11302283.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!