为什么最终的Java类中允许使用受保护的成员? [英] Why are protected members allowed in final java classes?

查看:58
本文介绍了为什么最终的Java类中允许使用受保护的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在最后的课程中允许受保护的成员?

Why are protected members allowed in final classes?

这不是编译时错误吗?

正如人们指出的那样,您可以使用默认修饰符来获得相同的程序包访问权限.它应该以完全相同的方式运行,因为protected只是默认+子类,而final修饰符明确地拒绝了子类,因此我认为答案不仅仅是提供相同的程序包访问.

as people have pointed out, you can get same package access by using the default modifier instead. It should behave in exactly the same manner, because protected is just default + sub-classes, and the final modifier explicitly denies subclassing, so I think the answer is more than just to provide same package access.

推荐答案

对于从基类覆盖protected方法的方法,而又不将那些成员暴露给public的方法,必须使用protected修饰符.

The protected modifier is necessary on methods that override protected methods from a base class, without exposing those members to the public.

通常,您可以引入很多不必要的规则来取缔不合逻辑的组合(例如protected static),但这并没有太大帮助.你不能禁止愚蠢.

In general, you could introduce a lot of unnecessary rules to outlaw implausible combinations (such as protected static), but it wouldn't help much. You can't outlaw stupidity.

这篇关于为什么最终的Java类中允许使用受保护的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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