是否可以在 Java 中覆盖同一类中的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11011992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:25:34  来源:igfitidea点击:

Is it possible to override a method in same class in Java?

javaoopinheritanceoverriding

提问by Debadyuti Maiti

Take a look at the following code sample :

看看下面的代码示例:

public class Test{

   public static void main(String[] args){

        System.out.println(new Test());


       System.out.println(new Test(){

        public String toString(){
             return "manual override";
         }

       });



      System.out.println(new Test(){

              public String gm(){
                    return  "manual gm";
               }
        }.gm());
     }  //end of main method

   public String gm(){
     return "gm";
    }
}

There may be some argument that the toString()method is being overridden in anonymous inner class which is an entirely different class.
But the overriding code still resides in the same class. So, will it be justified to conclude that in some situations [as described above] , the overriding of a method in same class is possible?

可能有人认为该toString()方法在匿名内部类中被覆盖,而匿名内部类是一个完全不同的类。
但是覆盖代码仍然驻留在同一个类中。那么,在某些情况下[如上所述],可以覆盖同一类中的方法是否合理?

回答by Kshitij

Firstly, you haven't defined toString in your Test.javaclass.

首先,你没有在你的Test.java类中定义 toString 。

Secondly, when you make a anonymous class, its is conceptually like creating a subclass. So overriding in anonymous class is allowed as long as parent method is not final.

其次,当你创建一个匿名类时,它在概念上就像创建一个子类。因此,只要父方法不是最终的,就允许在匿名类中进行覆盖。

Mainly, overriding is NOT possible in same class otherwise.

主要是,否则在同一类中不可能覆盖。

回答by Premraj

The answer is No, you cannot override the same method in one class. The anonymous inner class is a different class.

答案是否定的,您不能在一个类中覆盖相同的方法。匿名内部类是一个不同的类。

回答by Chandra Sekhar

The code above overrides toString()method of Objectclass. So you cannot say that it overrides in the same class. Now also it is overriding a superclass method and here the super class is Objectwhich is the super class of all classes.

上面的代码覆盖了Object类的toString()方法。所以你不能说它在同一个类中覆盖。现在它也覆盖了一个超类方法,这里的超类是Object,它是所有类的超类。

回答by amicngh

No , You can override a method in subclass only.

不,您只能覆盖子类中的方法。

public String toString(){
         return "manual override";
     }

In your case you are overriding Object'stoString()method not Testclass method.

在您的情况下,您正在覆盖Object'stoString()方法而不是Test类方法。

回答by swapy

In one class we can not have method with same signature. this is because there is no need to have override method in same class.

在一个类中,我们不能有具有相同签名的方法。这是因为在同一个类中不需要覆盖方法。

hence overriding method in same class is not possible, where as if we want to use same method name we can overload method by changing signature.

因此在同一个类中覆盖方法是不可能的,就像我们想使用相同的方法名称一样,我们可以通过更改签名来重载方法。

回答by user2494386

About the "real world use case," I do find a practical usage of the above program in section 2.5 of "JUnit Recipes" 2005 by Manning.

关于“真实世界用例”,我确实在 Manning 2005 年“JUnit 食谱”的第 2.5 节中找到了上述程序的实际用法。

回答by AmbiKesh

public class Counter {
private int count;
// a simple integer instance variable
public Counter( ) { }
// default constructor (count is 0)
public Counter(int initial) { count = initial; }
// an alternate constructor
public int getCount( ) { return count; }
// an accessor method
public void increment( ) { count++; }
// an update method
public void increment(int delta) { count += delta; }
// an update method
public void reset( ) { count = 0; }
// an update method
}

Isn;t this function overriding under same class??

这个函数不是在同一个类下覆盖吗??