何时不重写时调用super()方法?时调、重写、方法、super

2023-09-13 02:30:05 作者:Wretch 可怜虫

当我把我自己的Andr​​oid定制类,I 延长的本机类。然后,当我要重写基方法,我总是叫超()的方法,就像我一直做的的onCreate 的onStop 等。

When I make my own Android custom class, I extend its native class. Then when I want to override the base method, I always call super() method, just like I always do in onCreate, onStop, etc.

和我想这是它,因为从一开始Android团队建议我们始终调用上的每个方法重写。

And I thought this is it, as from the very beginning Android team advised us to always call super on every method override.

不过,在很多书我可以看到,开发者,比我更有经验,往往忽略调用,我真怀疑他们这样做作为知识的缺乏。例如,看一下这个基本 SAX解析器类,其中是省略的startElement 字符的endElement

But, in many books I can see that developers, more experienced than myself, often omit calling super and I really doubt they do it as a lack of knowledge. For example, look at this basic SAX parser class where super is omitted in startElement, characters and endElement:

public class SAXParser extends DefaultHandler{
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(qName.equalsIgnoreCase("XXY")) {
            //do something
        }
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        //do something
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(qName.equalsIgnoreCase("XXY")) {
            //do something
        }else () {
            //do something
        }
    }
}

如果您尝试创建通过Eclipse或任何其它IDE中的任何覆盖方法,将始终创建作为自动化过程的一部分。

If you try to create any override method via Eclipse or any other IDE, super will always be created as a part of automated process.

这只是一个简单的例子。书籍是全类似code

This was just a simple example. Books are full of similar code.

他们怎么知道,当你必须调用,当你可以忽略它调用?

How do they know when you must call super and when you can omit it calling?

PS。不结合该特定例子。这只是一个例子随机地从很多例子回升。

PS. Do not bind to this specific example. It was just an example randomly picked from many examples.

(这听起来像一个初学者的问题,但我真的很困惑。)

(This may sound like a beginner question, but I am really confused.)

推荐答案

通过调用方法,你不在的覆盖的行为该方法的,你的延长的吧。

By calling the super method, you're not overriding the behavior of the method, you're extending it.

一个调用将执行你扩展类定义了该方法的任何逻辑。考虑到这可能是重要的时刻,当你调用你的方法覆盖的实施。例如:

A call to super will perform any logic the class you're extending has defined for that method. Take into account that it might be important the moment when you call super's implementation in your method overriding. For instance:

public class A { 
    public void save() { 
         // Perform save logic
    }
}

public class B extends A {
    private Object b;
    @Override
    public void save() { 
        super.save(); // Performs the save logic for A
        save(b); // Perform additional save logic
    }
}

一个调用 B.save()将执行保存()逻辑为 A B ,在这个特定的顺序。如果你不叫 super.save() B.save() A.save()就不叫。如果你叫 super.save()保存(B) A.save ()将被有效地执行后 B.save()

A call to B.save() will perform the save() logic for both A and B, in this particular order. If you weren't calling super.save() inside B.save(), A.save() wouldn't be called. And if you called super.save() after save(b), A.save() would be effectively performed afterwards B.save().

如果你想的覆盖的的行为(即,完全忽略了它的实现,并提供了一​​切你自己),你不应该 T为调用

If you want to override super's behavior (that is, fully ignore its implementation and provide it all yourself), you shouldn't be calling super.

的SAXParser 比如你提供的implementations对这些方法的的DefaultHandler 都只是空的,这样子类可以重写它们,并提供这些方法的行为。在javadoc这种方法这也指出了。

In the SAXParser example you provide, the implementations of DefaultHandler for those methods are just empty, so that subclasses can override them and provide a behavior for those methods. In the javadoc for this method this is also pointed out.

public void startElement (String uri, String localName,
    String qName, Attributes attributes) throws SAXException {
    // no op
}

关于超()通过集成开发环境产生的,如 @barsju code默认来电指出,在他的意见,在每一个构造函数有一个隐式调用超()(即使你没有在code写的),这意味着,在这背景下,调用的默认构造函数。该 IDE 只是写下来给你,但它也将得到,如果你删除它叫。还要注意,实现构造函数时,超()或任何其带参数的变体(即超(X,Y,Z))只能被称为的在方法一开始就的。

About the super() default call in code generated by IDEs, as @barsju pointed out in his comment, in each constructor there's an implicit call to super() (even if you don't write it in your code), which means, in that context, a call to super's default constructor. The IDE just writes it down for you, but it would also get called if you removed it. Also notice that when implementing constructors, super() or any of its variants with arguments (i.e. super(x,y,z)) can only be called at the very beginning of the method.

 
精彩推荐
图片推荐