Android @Override 用法

发布于 2024-09-03 12:55:50 字数 367 浏览 2 评论 0原文

(Java 新手,C# 老手。)

我注意到 Android 示例代码中大量使用了 @Override。我认为所有 Java 方法默认都是“虚拟”的?

那么@Override 会做什么呢?

例子:

private class HelloWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

(Newbie to Java, old time C# guy.)

I have noticed a lot of the use of @Override in Android example code. I thought that all Java methods were by default "Virtual"?

What then does @Override do?

Example:

private class HelloWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流云如水 2024-09-10 12:55:50

这是一个注释,您可以使用它告诉编译器和 IDE 您希望具有该注释的方法覆盖超类方法。它们会发出警告/错误,以防您犯错误,例如,如果您打算覆盖某个方法但拼写错误,如果注释存在,IDE 或编译器会告诉您事实上它不是重写超类方法,因此您可以确定原因并纠正拼写错误。

例如,这对于 Android 应用程序和活动来说更为重要,其中所有调用都将基于活动生命周期 - 如果您没有正确重写生命周期方法,框架将永远不会调用它们。一切都会编译良好,但您的应用程序将不会按您预期的方式运行。如果添加注释,则会出现错误。

It's an annotation that you can use to tell the compiler and your IDE that you intend the method that has that annotation to be an override of a super class method. They have warning/errors in case you make mistakes, for example if you intend to override a method but misspell it, if the annotation is there the IDE or the compiler will tell you that it is not in fact overriding the super class method and thus you can determine why and correct the misspelling.

This is all the more important for Android applications and activities for example, where all of the calls will be based on the activity lifecycle - and if you do not properly override the lifecycle methods they will never get called by the framework. Everything will compile fine, but your app will not work the way you intend it to. If you add the annotation, you'll get an error.

奢欲 2024-09-10 12:55:50

这段代码对于真正想了解@Override过程的初学者来说,这会对你有所帮助! (提醒一下 Java 的继承概念。)

例如,Fish 类可能有两个子类:FreshwaterFishSaltwaterFish

这些子类将具有 Fish 类的所有功能,但可以通过父类 Fish 的新属性和行为或修改的行为进一步自定义对象。例如,FreshwaterFish 类可能包含有关所生活的淡水环境类型的信息(例如河流、湖泊、池塘或水坑)。

类似地,SaltwaterFish 类可能会自定义 makeBabyFish() 方法,以便鱼在繁殖后(如超类中定义)通过使用覆盖机制吃掉它的配偶,例如这:

public class SaltwaterFish extends Fish  
{  
    @Override  
    public void makeBabyFish(Fish fishSpouse, int numBabies) {  
        // call parent method  
        super.makeBabyFish(fishSpouse, numBabies);  
        // eat mate  
        eat(fishSpouse);  
    }  
}

This code for the beginner who really want to understand about the @Override process, this will help you! (Remind inheritance concept of Java.)

For example, the Fish class might have two subclasses: FreshwaterFish and SaltwaterFish.

These subclasses would have all the features of the Fish class, but could further customize the objects through new attributes and behaviors or modified behaviors from the parent class Fish. For example, the FreshwaterFish class might include information about the type of freshwater environment lived in (e.g. river, lake, pond, or puddle).

Similarly, the SaltwaterFish class might customize the makeBabyFish() method such that the fish eats its mate after breeding (as defined in the super class) by using the override mechanism, like this:

public class SaltwaterFish extends Fish  
{  
    @Override  
    public void makeBabyFish(Fish fishSpouse, int numBabies) {  
        // call parent method  
        super.makeBabyFish(fishSpouse, numBabies);  
        // eat mate  
        eat(fishSpouse);  
    }  
}
困倦 2024-09-10 12:55:50

Override-Annotation 只是向编译器提示您要覆盖某个函数。然后,编译器将检查父类和接口中是否存在该函数。如果没有,您将收到编译错误。

它基本上只是一种安全机制。

有关参考,请参阅 这篇文章(覆盖在中间的某个地方进行了解释)

The Override-Annotation is just a hint for the compiler that you want to overwrite a certain function. The compiler will then check parent-classes and interfaces if the function exists there. If not, you will get a compile-error.

Its basically just a safety mechanism.

For reference, see this article (override is explained somewhere in the middle)

鱼窥荷 2024-09-10 12:55:50

Override主要用于定义方法的情况。Overriding和way的含义类似。

我将尝试以非常蹩脚的方式进行解释。假设您已经定义了 Oncreate() 方法并具有与其关联的属性。再次,当您在代码中为某个对象调用 Oncreate() 方法时,您现在编写的代码...将覆盖应用程序的 Oncreate() 的正式定义属性或继承属性。

Override is mostly used in case of defining a method.Overriding is similar to way its meaning.

I will try to explain in very lame way.Suppose if you have Oncreate() Method already defined and have the associated properties with it. and Again when you call Oncreate() method in your code for certain object,the code which you have written now... will override the formally defined property or inherited property of Oncreate() for your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文