什么是受保护的方法在Java中的final类用例? [英] What's the use case of a protected method in a final class in Java?

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

问题描述

考虑从官方的这个code <一个href=\"http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/classes/java/awt/font/TextLayout.java\"相对=nofollow>中的OpenJDK源 java.awt.font.TextLayout中的

Consider this code from the official OpenJDK source of java.awt.font.TextLayout:

public final class TextLayout {

    /* ... */

    protected void handleJustify(float justificationWidth) {
      // never called
    }
}

这里有什么用例,它为什么可能是有意义的写code一样,在一般?

What's the use case here and why might it make sense to write code like that in general?

推荐答案

保护成员仍然可以由code来自同一个包访问。我的猜测是,类曾经是非最终在一些早期(甚至不公开)版本,那么,并作出终审判决,并且受保护的方法如此保持,因为有可能在使用它的同一个包code (而不是改制为民营仅仅包,因为没有人看见这样做一个好处)。

protected members can still be accessed by code from the same package. My guess is that the class used to be non-final in some earlier (perhaps not even public) version, was then made final, and the protected method kept as such because there might be code in the same package that uses it (and not changed to package private simply because nobody saw a benefit from doing so).

这篇关于什么是受保护的方法在Java中的final类用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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