在受保护的 void 超级方法中返回

huangapple go评论107阅读模式
英文:

Returning in protected void super method

问题

In the CooldownSpell class, you can modify the onActivate method to return a boolean value indicating whether the activation was successful. Then, in your subclasses, you can check this boolean value and decide whether to continue with their specific logic. Here's an updated version of your code to achieve this:

public class CooldownSpell extends Spell {
    protected int cooldownTicks = 0;
    protected int maxCooldownTicks = 0;

    // Modify the onActivate method to return a boolean
    protected boolean onActivate(Player p) {
        if (cooldownTicks > 0) {
            return false; // Activation failed
        } else {
            cooldownTicks = maxCooldownTicks;
            return true; // Activation successful
        }
    }

    public CooldownSpell(int cooldown) {
        this.maxCooldownTicks = cooldown;
    }

    // ...
}

public class DamageSpell extends CooldownSpell {
    private double radius;
    private double damage;

    public DamageSpell(int cooldown, double radius, double damage) {
        super(cooldown);
        this.maxCooldownTicks = cooldown;
        this.radius = radius;
        this.damage = damage;
    }

    @Override
    public void onActivate(Player p) {
        // Check if the activation was successful in the superclass
        if (super.onActivate(p)) {
            // Continue with the spell code
        }
    }
}

By making onActivate return a boolean, you can control whether the activation succeeds or fails in subclasses. In the DamageSpell class, it checks the return value of super.onActivate(p) and proceeds with its logic only if the activation was successful in the CooldownSpell superclass.

英文:

I'm making a spell system for a Minecraft Spigot plugin and I have many layers of inheritance.

public class Spell {
    protected void onActivate(Player p) {

    }

    public Spell() {

    }
}

This is my base Spell class. Then, I have a class for spells with cooldowns.

public class CooldownSpell extends Spell {
    protected int cooldownTicks = 0;
    protected int maxCooldownTicks = 0;
    protected void onActivate(Player p) {
        super.onActivate(p);
        if (cooldownTicks > 0) {
            return;
        } else {
            cooldownTicks = maxCooldownTicks;
            
        }

    }

    public CooldownSpell(int cooldown) {
        this.maxCooldownTicks = maxCooldownTicks;
    }

    // I am using a Bukkit scheduler to decrease cooldown ticks every tick
    public void decreaseCooldownTicks() {
        cooldownTicks--;
    }

    public int getMaxCooldownTicks() {
        return maxCooldownTicks;
    }

    public void setMaxCooldownTicks(int maxCooldownTicks) {
        this.maxCooldownTicks = maxCooldownTicks;
    }
}

For those who don't know, a tick in Minecraft is 1/20 of a second.

In onActivate(), there is an if statement: if cooldownTicks is greater than 0, I want to return from this method and all methods calling super.onActivate(). However, this doesn't work in subclasses.

public class DamageSpell extends CooldownSpell {
    private double radius;
    private double damage;

    public DamageSpell(int cooldown, double radius, double damage) {
        super(cooldown);
        this.maxCooldownTicks = cooldown;
        this.radius = radius;
        this.damage = damage;
    }

    @Override
    public void onActivate(Player p) {
        super.onActivate(p);
        // spell code stuff here
    }

Using the above code, which is a spell that inherits from CooldownSpell, I want to have to same cooldown system as the superclass, but the return statement only returns from the superclass method, not the DamageSpell method.

How can I make the return statement in the onActivate method of the CooldownSpell class return from the onActivate method in any subclasses?

答案1

得分: 1

以下是翻译好的代码部分:

目前我所知没有确切的方法可以这样做但你可以这样做

public class CooldownSpell extends Spell {
    public final void onActivate(Player p) {
        super.onActivate(p);
        if (cooldownTicks > 0) {
            return;
        } else {
            cooldownTicks = maxCooldownTicks;
        }
        this._onActivate(p);
    }
    protected void _onActivate(Player p){}
}

public class DamageSpell extends CooldownSpell {
    private double radius;
    private double damage;

    public DamageSpell(int cooldown, double radius, double damage) {
        super(cooldown);
        this.maxCooldownTicks = cooldown;
        this.radius = radius;
        this.damage = damage;
    }

    @Override
    protected void _onActivate(Player p) {
        // 在这里添加法术代码
    }
}

然后你只需调用 `onActivate(p)`,如果它不提前返回将调用你的 `_onActivate(p)` 方法
英文:

There's no way to do that exactly (as far as I know), but what you could do:

public class CooldownSpell extends Spell {
    public final void onActivate(Player p) {
        super.onActivate(p);
        if (cooldownTicks > 0) {
            return;
        } else {
            cooldownTicks = maxCooldownTicks;
        }
        this._onActivate(p);
    }
    protected void _onActivate(Player p){}
}

public class DamageSpell extends CooldownSpell {
    private double radius;
    private double damage;

    public DamageSpell(int cooldown, double radius, double damage) {
        super(cooldown);
        this.maxCooldownTicks = cooldown;
        this.radius = radius;
        this.damage = damage;
    }

    @Override
    protected void _onActivate(Player p) {
        // spell code stuff here
    }
}

And then you just call onActivate(p) which, if it doesn't return early, will call your _onActivate(p) method.

huangapple
  • 本文由 发表于 2020年8月6日 04:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63272861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定