简体   繁体   English

静态方法的受保护修饰符的目的是什么

[英]What is the purpose of protected modifier for static methods

I found such an usage of protected modifier while searching for a solution for my other question: Ignoring case in strings with unitils ReflectionComparator我在为我的另一个问题寻找解决方案时发现了这种protected修饰符的用法: Ignoring case in strings with unitils ReflectionComparator

In the org.unitils.reflectionassert.ReflectionComparatorFactory class there is a method with the signature:org.unitils.reflectionassert.ReflectionComparatorFactory类中有一个带有签名的方法:

protected static List<Comparator> getComparatorChain(Set<ReflectionComparatorMode> modes)

But this is only a particular case.但这只是一个特例。

After all we can always extend such any non-final class and "override" it's static protected method with the new public modifier.毕竟我们总是可以扩展这样的任何非 final 类并使用新的public修饰符“覆盖”它的静态protected方法。 Say, we have a class A :说,我们有一个A类:

public class A {
    protected static void test() {
        // do some stuff
    }
}

and want to use it in another package:并想在另一个包中使用它:

public class UseA {
    private static class MyA extends A {
        public static void test() {
            A.test();
        }
    }

    void useA() {
        // A.test(); compile error, sure
        MyA.test();
    }
}

I concentrate my question on a general situation when some static method was declared as protected .当某些static方法被声明为protected时,我将我的问题集中在一般情况上。 I'm not asking about non-static fields or methods, because in some cases class can have a private constructor or a very complicated constructor with lots special params.我不是在问非静态字段或方法,因为在某些情况下,类可以有一个私有构造函数或一个非常复杂的构造函数,其中包含许多特殊参数。 But what is the purpose of such "hiding" static methods if entire class isn't final ?但是,如果整个类不是final那么这种“隐藏”静态方法的目的是什么? Is such usage an OOP mistake or just a very weak "protection"?这种用法是 OOP 错误还是只是非常薄弱的​​“保护”?

But what is the purpose of such "hiding" static methods if entire class isn't final?但是如果整个类不是最终的,那么这种“隐藏”静态方法的目的是什么?

A protected static method would allow you to provide "utility" type functionality to derived classes, without exposing them in the public API where they might not make sense on their own. protected static方法将允许您为派生类提供“实用程序”类型的功能,而不会将它们暴露在公共 API 中,因为它们本身可能没有意义。

I don't know the implementation of the getComparatorChain method you reference, but I imagine it's such a method.我不知道你引用的getComparatorChain方法的实现,但我想它是这样一个方法。 It would be marked static if it's not tied to any specific instance, and marked protected so as not to be a part of the public API, but also to allow derived classes to re-use the utility method in it's own implementation.如果它不绑定到任何特定实例,它将被标记为static ,并被标记为protected以便不成为公共 API 的一部分,但也允许派生类在其自己的实现中重用实用程序方法。

Overriding a static method reference means hiding it's implementation.覆盖静态方法引用意味着隐藏它的实现。 See: Java Documentation's Overriding and Hiding Methods请参阅:Java 文档的覆盖和隐藏方法

Static Methods静态方法

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.如果子类定义了与超类中的静态方法具有相同签名的静态方法,则子类中的方法隐藏超类中的方法。

The distinction between hiding a static method and overriding an instance method has important implications:隐藏静态方法和覆盖实例方法之间的区别具有重要意义:

  • The version of the overridden instance method that gets invoked is the one in the subclass.被调用的重写实例方法的版本是子类中的版本。
  • The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.被调用的隐藏静态方法的版本取决于它是从超类还是从子类调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM