【问题标题】:Why does my IDE say my piece of code isnt extending a method of its superclass, even though it is?为什么我的 IDE 说我的代码没有扩展其超类的方法,即使它是?
【发布时间】:2021-12-24 07:23:12
【问题描述】:

我最近在 Java 中使用 IntelliJ IDEA 进行编码,尤其是 Minecraft。

我尝试扩展 AbstractSkeletonEntity 类,它是一个抽象类,具有一种最近对我来说很麻烦的特定方法:

abstract SoundEvent getStepSound();

在我自己的代码中,我尝试覆盖该方法:

public class SkeletonKnightEntity extends AbstractSkeletonEntity {



public boolean SkeletonCanSpawn = true;

public SkeletonKnightEntity(EntityType<? extends AbstractSkeletonEntity> entityType, World world) {
    super(entityType, world);

}

protected SoundEvent getAmbientSound() {
    return SoundEvents.ENTITY_WITHER_SKELETON_AMBIENT;
}

protected SoundEvent getHurtSound(DamageSource source) {
    return SoundEvents.ENTITY_WITHER_SKELETON_HURT;
}

protected SoundEvent getDeathSound() {
    return SoundEvents.ENTITY_WITHER_SKELETON_DEATH;
}

@Override
protected void playStepSound(BlockPos pos, BlockState state) {
    this.playSound(this.getStepSound(), 0.15F, 1.0F);
}

@Override
protected SoundEvent getStepSound() { return SoundEvents.ENTITY_WITHER_SKELETON_STEP;}
//^ here is the error

@Override
public void attack(LivingEntity target, float pullProgress) {

}
@Override
public void tickMovement() {
    super.tickMovement();
}
public static DefaultAttributeContainer.Builder createSKnightAttributes(){
    return HostileEntity.createHostileAttributes()

            .add(EntityAttributes.GENERIC_MOVEMENT_SPEED, 0.25D)
            .add(EntityAttributes.GENERIC_FOLLOW_RANGE, 30)
            .add(EntityAttributes.GENERIC_ATTACK_DAMAGE, 1)

            ;
}
@Override
protected void initGoals() {
    this.goalSelector.add(3, new FleeEntityGoal(this, WolfEntity.class, 6.0F, 1.0D, 1.2D));
    this.goalSelector.add(5, new WanderAroundFarGoal(this, 1.0D));
    this.goalSelector.add(6, new LookAtEntityGoal(this, PlayerEntity.class, 8.0F));
    this.goalSelector.add(6, new LookAroundGoal(this));
    this.targetSelector.add(1, new RevengeGoal(this, new Class[0]));
    this.targetSelector.add(2, new FollowTargetGoal(this, PlayerEntity.class, true));
    this.targetSelector.add(3, new FollowTargetGoal(this, IronGolemEntity.class, true));
    this.targetSelector.add(3, new FollowTargetGoal(this, TurtleEntity.class, 10, true, false, TurtleEntity.BABY_TURTLE_ON_LAND_FILTER));
}
protected void dropEquipment(DamageSource source, int lootingMultiplier, boolean allowDrops) {
    super.dropEquipment(source, lootingMultiplier, allowDrops);
    Entity entity = source.getAttacker();
    if (entity instanceof CreeperEntity) {
        CreeperEntity creeperEntity = (CreeperEntity)entity;
        if (creeperEntity.shouldDropHead()) {
            creeperEntity.onHeadDropped();
            this.dropItem(Items.SKELETON_SKULL);
        }
    }

}
}

IDE 告诉我要让代码工作,我必须声明 SkeletonKnightEntity 抽象或实现我已经做过的抽象方法 getStepSound()。当我尝试覆盖 getStepSound() 方法时,它告诉我它没有覆盖超类中的任何内容。有谁知道是什么原因造成的?

【问题讨论】:

  • 这是问题所在:SoundEvent getStepSound();(包可见性)!= protected SoundEvent getStepSound()

标签: java minecraft minecraft-fabric


【解决方案1】:

当你重写一个方法时,你可以增加它的可见性,但是你不能减少它。您采用了 public 方法并用 protected 覆盖了它。这是不允许的。超类承诺可以从任何地方调用该方法,而您给了它一个不符合该承诺的方法。删除 protected 修饰符就可以了。

您没有明确说明,所以为了完整起见,我将其包括在内。我假设父方法是public。如果它确实没有修饰符(如您的问题所示),那么它会获得“package-private”的默认可见性。包私有方法根​​本无法在定义它的包之外访问,因此如果该类的作者创建了一个抽象包私有方法,那么他们打算不允许定义包之外的任何人访问子类化他们的类,所以你也不会被允许。

请参阅this post,了解 Java 中可见性选项的摘要。

【讨论】:

  • 谢谢,但这似乎不起作用。仍然说我没有覆盖它
  • 我刚刚意识到我的答案不完整。请参阅我刚刚添加的第二段。您要覆盖包私有的方法吗?
  • 是的,它是私有包
  • 那么不幸的是你有点不走运。该类的作者打算只在同一个包中覆盖它(有点像 Kotlin 或 Scala 中的 sealed 关键字),并且没有办法解决这个问题。
  • 我可以创建自己的类来复制它并使用它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多