Java 该方法必须覆盖或实现超类型方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26110425/
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-08-11 01:51:50  来源:igfitidea点击:

The method must override or implement a supertype method

javaminecraftminecraft-forge

提问by GriffinMite

I am making a custom armor, and in my armor class I am getting this error:

我正在制作自定义盔甲,在我的盔甲类中,我收到此错误:

The method getArmorTexture(ItemStack, Entity, int, int) of type ArmorE must override or implement a supertype method

ArmorE 类型的 getArmorTexture(ItemStack, Entity, int, int) 方法必须覆盖或实现超类型方法

Why I am getting this error?

为什么我收到这个错误?

Here's my code:

这是我的代码:

Armor Class:

装甲等级:

package com.domoq.EmeraldGear.armor;

import com.domoq.EmeraldGear.EmeraldGearMod;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;

public class ArmorE extends ItemArmor {

    public ArmorE(ArmorMaterial part2ArmorE, int part3, int part4) {
        super(part2ArmorE, part3, part4);
        this.setCreativeTab(CreativeTabs.tabCombat);
    }

    @Override
    public String getArmorTexture(ItemStack stack, Entity entity, int slot, int type) {
        if (stack.getItem() == EmeraldGearMod.EmeraldHelmet || stack.getItem() == EmeraldGearMod.EmeraldChest || stack.getIconIndex() == EmeraldGearMod.EmeraldBoots) {
            return "emeraldgearmod:textures/models/armor/ArmorL1.png";
        } else if (stack.getItem() == EmeraldGearMod.EmeraldLegs) {
            return "emeraldgearmod:textures/models/armor/ArmorL2.png";
        } else return null;
    }
}

Part of Main Class:

主类的一部分:

//Armor Material
public static ArmorMaterial ArmorE = EnumHelper.addArmorMaterial("AEmerald", 29, new int[]{3, 7, 4, 2}, 25);

//Armor Items
public static Item EmeraldHelmet = new ArmorE(ArmorE, 2, 0).setUnlocalizedName("EmeraldHelmet").setTextureName("emeraldgearmod:emerald_helmet");
public static Item EmeraldChest = new ArmorE(ArmorE, 2, 1).setUnlocalizedName("EmeraldChest").setTextureName("emeraldgearmod:emerald_chestplate");
public static Item EmeraldLegs = new ArmorE(ArmorE, 2, 2).setUnlocalizedName("EmeraldLegs").setTextureName("emeraldgearmod:emerald_leggings");
public static Item EmeraldBoots = new ArmorE(ArmorE, 2, 3).setUnlocalizedName("EmeraldBoots").setTextureName("emeraldgearmod:emerald_boots");

采纳答案by Reimeus

To override a method the signature needs to match that of the super class. Replace

要覆盖方法,签名需要与超类的签名相匹配。代替

public String getArmorTexture(ItemStack stack, Entity entity, int slot, int type) {

with

public String getArmorTexture(ItemStack stack, Entity entity, int slot, String type) {

回答by PsyCode

It means that you don't need the override annotation since you aren't overriding or implementing something to that method. Therefore, you should merely delete

这意味着您不需要覆盖注释,因为您没有覆盖或实现该方法的某些内容。因此,您应该只删除

@Override

回答by sofs1

If you are using Eclipse, try closing and opening it again. The error goes away.

如果您使用的是 Eclipse,请尝试关闭并再次打开它。错误消失了。

回答by Rare Case

In your defined interface ItemArmor remove any generic type in angle brackets.

在您定义的接口 ItemArmor 中,删除尖括号中的任何泛型类型。

回答by Niamatullah Bakhshi

If you are working on Java Spring Framework then you must provide the using of annotations by writing <context:annotation-config/>in your i.e config.xmlfile so to be able to use Annotations such as @Override,@Component,@Valueand etc.

如果您在Java的Spring框架的工作,那么你必须通过编写提供注解的使用<context:annotation-config/>在你的IEconfig.xml文件,以便能够使用注释,如@Override@Component@Value等。

Right-click on your package and create a .xml(i.e config.xml) file for configuration if not exist.

右键单击您的包并创建一个 .xml(即 config.xml)文件用于配置(如果不存在)。

and Add the following code inside it.

并在其中添加以下代码。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- This allows annotations -->
    <context:annotation-config />


</beans>

回答by Wincent

You may have unsaved content in Interface or Base-Class. This is why delete annotation @Overridecan clean the error.

您可能在 Interface 或 Base-Class 中有未保存的内容。这就是为什么删除注释@Override可以清除错误的原因。

回答by corsiKa

I was brought here by a Google search and while this won't help OP (since clearly their issue is solved in the accepted answer) I want to share what my problem was for potential future visitors.

我是通过 Google 搜索来到这里的,虽然这对 OP 没有帮助(因为他们的问题显然已在已接受的答案中解决),但我想与潜在的未来访问者分享我的问题。

I was losing my mind - I was working with Jetty and had a method that just would not accept the Override annotation. I even COPIED AND PASTED it from the base class to ensure I hadn't mangled the method signature some how. And while many answers said ignore it, it's the entry point to a websocket and it wasn't firing - clearly, I can't ignore that!

我正在失去理智 - 我正在与 Jetty 合作,并且有一种方法不会接受 Override 注释。我什至从基类复制并粘贴了它,以确保我没有以某种方式破坏方法签名。虽然许多答案都说忽略它,但它是 websocket 的入口点,它没有触发 - 显然,我不能忽略它!

It ended up being the import - I was importing Session from the wrong package - from a Jetty package, so it sure looked legit, but I needed to have org.eclipse.jetty.websocket.api.Sessionwhile I had org.eclipse.jetty.server.session.Sessionin my imports. Future visitors are unlikely to have that pairing of mismatched package imports, but I suspect some are going to be in the boat I was just in.

它结束了进口-我是从错误的包导入会话-从码头包,所以它肯定看起来合法的,但我需要有org.eclipse.jetty.websocket.api.Session,而我org.eclipse.jetty.server.session.Session在我的进口。未来的访客不太可能拥有那对不匹配的包裹进口,但我怀疑有些人会在我刚刚乘坐的船上。