简体   繁体   中英

@Override annotation in Android

I am new to Java and Android programming. The issue that I am having is that after surfing several books, forum and websites, I do not have clear understanding of what the @override annotation does. I understand it signifies when a method is being over ridden. but why is it needed in android. I see it rarely in source code for java, but all the time in android.

You may see it rarely in old Java source code, because it's a fairly recent innovation - whereas Android code is more recent pretty much by definition.

It's a safety net, really - it tells the compiler that you're trying to override something - so please fail if the method doesn't override anything, eg due to a typo in the name. It's just like override being a keyword which is part of the method declaration in C#. It helps you to be explicit about what you're doing, which helps to prevent mistakes and also makes your code clearer to future readers.

Well if you understand the basic of @Override annotation it's simply metadata information that fill two purposes:

  • Describe that the method it's being override because it was defined in a interface or a extended class

  • Helps you identify correctly which method are locally declared and which are overriding since in android you will probably extends from Fragment, Activity, Services, BroadcastReceiver, etc it is a good practice that will make your code cleaner.

As for the issue that you don't see it in Java style, it is not only because it's a fairly new Feature (not that new since 2005), but because you don't override method that much in java at least not in Swing or SWT, most commonly you will find them in TableModel or when it's Servlet in the extended HttpServlet classes

因为你覆盖了很多方法,例如;当你创建你的Activity (扩展Activity )时,你需要覆盖方法onCreate( Bundle savedInstanceState)来初始化你的Activity ,依此类推, 每次你从一个母类重写一个方法,eclipse添加注释 @override

@Override means that you want to re-define function from superclass in child class. Read more about abstract methods, classes, polymorphism.

I know next to nothing about Android-programming, but the @Override-annotation is used to show that the method overrides a supeclass method (or implements an interface method in Java 1.6). Besides the purely documentational meaning of the annotation, at least Eclipse also warns you (or marks it an error, depending on your settings) if the method doesn't actually override any superclass method, when you think it does (thus preventing some frustrating bug-hunting). Maybe the Android-compiler just by default requires all overriding methods to be annotated with @Override.

@overide is something like a virtual method in c++, Oncreate method in android is more like the virtual bool Oninit() in wxWidget, this method is used by android (Oncreate) to build your GUI so because GUI is different from who is programming the code in Oncreate change too this is why we have to @overide it. We @overide any method that is used by the library and that we want to customize. Oncreat() like Oninit() are those method we want to customize too.

out_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:layout_marginRight="50dp"
            android:gravity="center"
            android:orientation="vertical">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/txtProgress"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:progress="50"
                android:progressTint="@android:color/black" />

            <TextView
                android:id="@+id/txtProgress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="Progress"
                android:textColor="@android:color/black" />
        </LinearLayout>  

Java

It is not needed for both java and android. You see it often in android programs because eclipse adds it automatically when you create a new activity, method, etc. If you delete the @Override annotation nothing will appen.

@override意味着您从可用类借用方法,并且不是由程序员定义的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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