简体   繁体   English

为什么在子类中没有完全限定名称的情况下可以访问静态方法?

[英]Why can a static method be accessed without a fully-qualified name in a subclass?

The following code demonstrates that the subclass named SubClass has a direct access to a final static synchronized method named staticMethod . 以下代码演示了名为SubClass的子SubClass可以直接访问名为staticMethod最终静态同步方法。 There is no need to associate it with its class name. 无需将其与其类名关联。

package synchronizedpkg;

class SuperClass
{
    public final static synchronized void staticMethod()
    {
        System.out.println("Method called.");
    }
}

final class SubClass extends SuperClass
{
    public void woof()
    {
        staticMethod();
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        new SubClass().woof();
    }
}

This is somewhat confusing in terms of inheritance because a final method can not be inherited and consequently should not directly be accessed by it's subclasses. 就继承而言,这有些令人困惑,因为无法继承final方法,因此不应由其子类直接访问final方法。 How does a final static method as shown above have a direct access from its child class? 上面显示的最终静态方法如何从其子类直接访问?

The static method is inherited just like instance methods. 静态方法就像实例方法一样被继承。 From section 8.4.8 of the JLS : JLS的8.4.8节开始

A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (§8.4.8.1) nor hidden (§8.4.8.2) by a declaration in the class. 类C从其直接超类和直接超接口继承超类的所有非私有方法(无论是抽象与否)和在与C相同的程序包中具有默认访问权限的公共,受保护或声明的超接口,并且都不会被覆盖(第8.4节) .8.1)或由类中的声明隐藏(§8.4.8.2)。

That doesn't say anything about only inheriting instance methods. 那仅说明了继承实例方法。

Your understanding is wrong: 您的理解是错误的:

This is somewhat confusing in terms of inheritance because a final method can not be inherited and consequently should not directly be accessed by it's subclasses. 就继承而言,这有些令人困惑,因为无法继承final方法,因此不应由其子类直接访问final方法。 How does a final static method as shown above have a direct access from its child class? 上面显示的最终静态方法如何从其子类直接访问?

A final method cannot be overridden , or in the case of a static method hidden , it is still inherited. 最终方法不能被覆盖 ,或者在静态方法被隐藏的情况下,它仍然被继承。 If you want to prevent the subclass from seeing it, then you must make it private . 如果要防止子类看到它,则必须将其设为private

So, it is visible, hence you can access it. 因此,它是可见的,因此您可以访问它。 And it is a member, hence you don't need to qualify it. 它是成员,因此您不需要限定它。

Static means which is same for all objects. 静态表示所有对象都相同。 So, it doesn't really matter if you call it with object reference or class reference because it is same for every object. 因此,使用对象引用或类引用调用它并不重要,因为每个对象都相同。

Example: Suppose you have teacher who teaches to all the students in all grades. 示例:假设您有一位老师向所有年级的所有学生授课。 So, it doesnt really matter if 1st grade student calls her 1st grade teacher and 2nd grade student calls her 2nd grade teacher or somebody else calls her school teacher. 因此,一年级学生打电话给她的一年级老师,而二年级学生打电话给她的二年级老师还是其他人打电话给她的学校老师,这并不重要。 They are all refering to same teacher. 他们都是指同一位老师。

Students can refer to her by name and every other student can know that he is refering to her. 学生可以按名字指称她,而每个其他学生都可以知道他在指称她。 But people from outside school, if they have to refer to her they have to say the school name and teachers name. 但是来自校外的人,如果必须参考她,就必须说出学校的名字和老师的名字。

That's how it works even in java. 即使在Java中也是如此。 Java is probably the only language which relates to real world scenarios. Java可能是唯一与现实世界场景相关的语言。 Hope this helps..... :) 希望这可以帮助..... :)

From Understanding Instance and Class Members : 通过了解实例成员和类成员

"The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in “ Java编程语言支持静态方法和静态变量。在声明中具有static修饰符的静态方法应使用类名调用,而无需创建类的实例,如

ClassName.methodName(args)

Note: You can also refer to static methods with an object reference like 注意:您还可以使用对象引用来引用静态方法,例如

instanceName.methodName(args)

but this is discouraged because it does not make it clear that they are class methods." 但这是不鼓励的,因为它并不清楚它们是类方法。”

Hence, you can call a static method from an instance that inherits it OR from its class name. 因此,您可以从继承其实例的实例中调用静态方法,或者从其类名称中调用静态方法。 This means that for all intents and purposes, you can imagine the above method as not being static, and you can inherit it normally. 这意味着,出于所有意图和目的,您可以将上述方法想象为不是静态的,并且可以正常继承它。

EDIT: As to the final modifier, this just means that the method cannot be overridden in a subclass. 编辑:至于最终的修饰符,这仅意味着该方法不能在子类中被覆盖。 Far as I can see, you aren't doing that, so it does not affect the outcome. 据我所知,您没有这样做,所以它不会影响结果。 You can read about this here . 您可以在这里阅读有关内容。

static表示类级别,它与类的对象无关,您不需要对象即可访问静态方法

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

相关问题 无法从Javadoc元素获取完全限定的类型名称 - Unable to Get Fully-Qualified Type Name from Javadoc Element JavaDoc提供完全限定的类名...如何缩写? - JavaDoc giving fully-qualified class name… how to abbreviate? Eclipse API:仅使用文件的名称字符串从IJavaProject获取IFile或标准名称 - Eclipse API: Get IFile or fully-qualified name from IJavaProject using only the file's name string 当前方法的完全限定方法名称 - Fully qualified method name of the current method 是“继承”正确的术语来解释超类的静态方法可以通过子类访问吗? - Is “inherited” the correct term to explain static method of superclass can be accessed by subclass? class 的完全限定名称? - Fully qualified name of a class? 如何在不首先实例化类的情况下获取完全限定的类名称? - How to get the fully qualified class name without instantiating the class first? 如何在方法中获取参数值的完全限定名称 - How to get fully qualified name of parameter value in a method 如何在Eclipse中按方法的全限定名搜索 - How to search by method's fully qualified name in Eclipse 为什么在JDK类中使用完全限定的名称声明Serializable? - Why is Serializable declared with fully qualified name in JDK classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM