简体   繁体   English

Java中的静态方法

[英]Static Methods in Java

I am new to Java Programming. 我是Java编程的新手。 Can anyone please explain me why the program outputs - "fa la" even though the static method is overridden. 任何人都可以解释我为什么程序输出 - “fa la”即使静态方法被覆盖。 I read that static methods can't be overridden in Java? 我读过静态方法不能在Java中重写? Please correct me if I am wrong. 如果我错了,请纠正我。

public class Tenor extends Singer {  
   public static String sing() {   
      return "fa";   
   }  
   public static void main(String[] args) {  
      Tenor t = new Tenor();  
      Singer s = new Tenor();  
      System.out.println(t.sing() + " " + s.sing());  
  }  
 }  
class Singer {   
   public static String sing() {   
       return "la";   
    }   
}  

You cannot override static methods in Java. 您无法在Java中覆盖静态方法。 It's simply calling the static methods directly on each class. 它只是直接在每个类上调用静态方法。 BTW: As others have noted, it's considered bad practice to call static methods on instances. 顺便说一句:正如其他人所说,在实例上调用静态方法被认为是不好的做法。 For clarity, you should do the following: 为清楚起见,您应该执行以下操作:

System.out.println(Tenor.sing() + " " + Singer.sing());

which is equivalent to the code you have written in your question. 这相当于你在问题中写的代码。

t is of type Tenor and s is of type Singer . tTenor类型, sSinger类型。

When you invoke a static method on an instance (which is bad practice) you are invoking the static method on the class of the declared type. 当您在实例上调用静态方法时(这是一种不好的做法),您将在声明的类型的类上调用静态方法。

ie t.sing() is equivalent to Tenor.sing() t.sing()相当于Tenor.sing()

Static methods should be accessed with the class name and not an object reference. 应使用类名而不是对象引用来访问静态方法。 The correct equivalent of what you wrote would be: 你所写的正确等价物是:

System.out.println(Tenor.sing() + " " + Singer.sing())

Java is guessing inferring which method you meant to invoke based on the type of object variable. Java 猜测根据对象变量的类型推断你要调用哪个方法。

EDIT: As Stephen pointed out, it isn't guessing. 编辑:斯蒂芬指出,这不是猜测。 Inferring would probably be a more accurate word. 推断可能是一个更准确的词。 I was just trying to emphasize that calling static functions on object references might result in behavior you wouldn't expect. 我只是想强调在对象引用上调用静态函数可能会导致您不期望的行为。 In fact, I just tried a few things and found out that my previous statement was incorrect: Java decides which method to call based on the variable type. 事实上,我只是尝试了一些事情并发现我之前的陈述不正确:Java根据变量类型决定调用哪个方法。 This is obvious now that I think about it more but I can see how it could lead to confusion if you did something like: 现在这很明显,我想的更多,但我可以看到如果你做了类似的事情,它会如何导致混乱:

Singer s = new Tenor();
System.out.println(s.sing());

Static methods are not overridden. 静态方法不会被覆盖。 They belong to the class in which they are defined. 它们属于定义它们的类。 Calling a static method on an instance of that method does work the same way as calling it on the class, but makes things less clear and leads to people thinking they are overridden like instance methods. 在该方法的实例上调用静态方法的方式与在类上调用它的方式相同,但会使事情变得不那么清晰,并导致人们认为它们像实例方法一样被覆盖。

Static methods can't be overridden. 静态方法无法覆盖。

the following resource will help you to understand better what is going on when you are trying override static method: 以下资源将帮助您更好地了解在尝试覆盖静态方法时发生的情况:

http://www.javabeat.net/qna/49-can-we-override-static-methods-what-is-metho/ http://www.javabeat.net/qna/49-can-we-override-static-methods-what-is-metho/

Regards, Cyril 此致,西里尔

The code you wrote works fine, but probably not how you intended it to. 你写的代码很好,但可能不是你想要的。 Static methods can't be overridden in the traditional sense (or with the behavior you might expect). 静态方法不能在传统意义上(或您可能期望的行为)被覆盖。 In terms of which version of the method is invoked, this is done at compile time , which explains the output you see. 就调用哪个版本的方法而言,这是在编译时完成的,这解释了您看到的输出。

Yes, and you should probably only invoke the methods using the Class name. 是的,您应该只使用类名调用方法。

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

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