注释掉“@Override”是否安全?

发布于 2024-12-24 19:41:22 字数 1266 浏览 1 评论 0原文

为了编译我的代码 - 其中包含以下内容:

public class ContactsActivity extends ListActivity implements AdapterView.OnItemClickListener {

    Cursor mContacts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Return all contacts, ordered by name
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME };
        mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);

        // Display all contacts in a ListView
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, mContacts,
                new String[] { ContactsContract.Contacts.DISPLAY_NAME },
                new int[] { android.R.id.text1 });
        setListAdapter(mAdapter);
        // Listen for item selections
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
. . .

...我必须注释掉 onItemClick() 事件处理程序上方的“@Override”。这安全吗?我应该耸耸肩说:“好吧,那行了,我会忘记它”还是有邪恶正在发生/隐藏的问题潜伏着?

To get my code to compile - which contained the following:

public class ContactsActivity extends ListActivity implements AdapterView.OnItemClickListener {

    Cursor mContacts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Return all contacts, ordered by name
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME };
        mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);

        // Display all contacts in a ListView
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, mContacts,
                new String[] { ContactsContract.Contacts.DISPLAY_NAME },
                new int[] { android.R.id.text1 });
        setListAdapter(mAdapter);
        // Listen for item selections
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
. . .

...I had to comment out the "@Override" above the onItemClick() event handler. Is this safe? Should I just shrug my shoulders and say, "OK, that works, I'll forget about it" or is there evil afoot/a hidden problem lurking?

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

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

发布评论

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

评论(6

做个少女永远怀春 2024-12-31 19:41:23

根据我的建议,删除该行不好。你需要在你的eclipse中更改JDK版本,这样你就不会出现这样的错误。按照以下步骤操作,

右键单击您的项目并单击它。选择属性。

然后从左侧部分选择 Java Compiler &选择您安装的 JDK 版本。如果已选择,则取消选择并取消选择。尝试一下。

as per my suggestion, it is not good to remove that line. you need to change to JDK version in your eclipse then you will not get such errors. Follow, following steps for it,

Right Click on your Project & select Properties.

Then from the left section Select Java Compiler & select the Version of the JDK you installed. If it is already selected then de-select it & try it.

看透却不说透 2024-12-31 19:41:23

如果您必须注释掉 @Override,那么您的编译器的 JDK 合规级别未设置为 1.6,而 Android 开发应设置为 1.6。如果您使用的是 Eclipse,请右键单击项目名称并选择“属性”。然后选择“Java Compiler”并根据需要进行调整。 (您可能从工作区继承了较低的 JDK 合规性级别。如果合适,请更改工作区的合规性级别。这样就不必在新项目中修复相同的问题。)

但是要回答您的问题:注释掉是安全的@Override。使用较低的合规级别并不是那么好。

If you had to comment out the @Override, then your compiler's JDK compliance level isn't set to 1.6, which it should be for Android development. If you're using Eclipse, right click on the project name and select "Properties". Then select "Java Compiler" and adjust as necessary. (You probably inherited a lower JDK compliance level from the workspace. If appropriate, change the compliance level for the workspace instead. This will save having to fix the same problem with new projects.)

But to answer your question: it's safe to comment out @Override. It's just not so great to be using a lower compliance level.

Spring初心 2024-12-31 19:41:23

你的java版本设置为低于1.6。从这里开始,接口(而不仅仅是方法)可以作为 Override 进行销售。右键单击项目->属性-> Java编译器->编译器合规级别为 1.6

You java version is set to less than 1.6. Starting there, interfaces and not just methods can be market as Override. Right click the project -> Properties -> Java Compiler -> Compiler compliance level to 1.6

太阳男子 2024-12-31 19:41:23

来自 Java 文档

@Override - @Override 注释通知编译器
元素旨在覆盖超类中声明的元素
(重写方法将在标题为“
“接口和继承”)。

   // mark method as a superclass method
   // that has been overridden
   @Override 
   int overriddenMethod() { }

虽然在重写方法时不需要使用此注释,但它有助于防止错误。如果一个方法被@Override标记
无法正确重写其超类之一中的方法,
编译器生成错误。

From the Java docs:

@Override - the @Override annotation informs the compiler that the
element is meant to override an element declared in a superclass
(overriding methods will be discussed in the the lesson titled
"Interfaces and Inheritance").

   // mark method as a superclass method
   // that has been overridden
   @Override 
   int overriddenMethod() { }

While it's not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override
fails to correctly override a method in one of its superclasses, the
compiler generates an error.

邮友 2024-12-31 19:41:23

这取决于。如果您的编译器设置为目标 java 5,并且 onCreate 方法是从接口继承的,那么可以。 Java 5 不支持接口方法上的 override 注解,而 Java 6 则支持。这本身可能表明您的 JDK 设置错误,如果您希望以 Java 6 为目标,那么您得到的可能是 Java 5。

It depends. If your compiler is set to target java 5, and the onCreate method is inherited from the interface then yes. Java 5 doesn't support the override annotation on interface methods, whereas Java 6 expects it. This may itself be an indication that your JDK is set up wrong, if you're expecting to target Java 6, what you may be getting is Java 5.

骷髅 2024-12-31 19:41:23

如果您将其注释掉,则不会覆盖这些方法。所以你实际上什么也不做。 (我的意见)

if you'd commented it out, you would not override the methods. So you'd actually do nothing. (My opinion)

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