在OOP中操作Java对象

细节

我试图在Java中操作对象。为了更好地说明,我有一个称为Creature的超类和两个名为(Dragon和Warrior)的子类。我为龙和战士彼此战斗创建了两个对象。如何设置对一个对象造成伤害的攻击方法,并使用该数字将其从第二个对象的生命值中减去。

请注意,战士和龙都是子类。

public class Creature {

    private int health = 50;
    private int attack;
    private int level = 1;
    private String name;
    private Creature random;
    private Creature random2;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHealth() {
        if(health >= 3000)
        {
            health = 3000;
        }
        if(health <= 0)
        {
            health = 0;
        }
        return health;
    }

    public void setHealth(int health) {
        this.health = health < 0 ? 0 : health;
    }

    public int getAttack() {
        Random generator = new Random();
        attack = generator.nextInt(10) * level + 1;
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getLevel() {
        if(level >= 60)
        {
            level = 60;
        }
        return level;
    }

    public void setLevel(int level){
        this.level = level;   
    }

    public boolean isAlive() {
        if(getHealth() <= 0)
        {
            return false;
        }
        return true;
    }







    public String getWelcome()
    {
        String welcome = "Hello and welcome to Dragonslayer!";
        return welcome;
    }

    public String getStab()
    {
        String stab = "You choose to stab the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
        return stab;
    }

    public String getSlash()
    {
        String slash = "You choose to slash the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
        return slash;
    }

    public String getCut()
    {
        String cut = "You choose to cut the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
        return cut;
    }  



    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("You come across the dragon and you have two options.  Do you run or fight? ");


        Creature kirsta = new Dragon();
        kirsta.setHealth(50);

        Creature brenton = new Warrior();
        brenton.setHealth(50);

        while(in.hasNextLine())
        {
            switch(in.nextLine())
            {
                case "run":
                    System.out.println("I am so sorry, you could not outrun the dragon, you have been killed!");
                    break;
                case "fight":
                    while(kirsta.isAlive() && brenton.isAlive())
                    {
                        System.out.println("Do you want to stab, slash, or cut the dragon? ");
                        switch(in.nextLine())
                        {
                            case "stab":
                                System.out.println("You choose to stab the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
                                break;
                            case "slash":
                                System.out.println("You choose to slash the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
                                break;
                            case "cut":
                                System.out.println("You choose to cut the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
                                break;
                            default:
                                System.out.println("I am sorry that is not valid, try again. ");
                        }
                    }
                    break;
                default:
                    System.out.println("I am sorry that is not a valid choice. ");
                    break;
            }//end of switch
            if(brenton.isAlive() == false && kirsta.isAlive() == false)
            {
                System.out.println("It is a horrid day today, as both you and the dragon have fallen.");
            }
            else if(brenton.isAlive() == true && kirsta.isAlive() == false)
            {
                System.out.println("Congratulations you have killed the dragon, and survived!");
            }
            else if(brenton.isAlive() == false && kirsta.isAlive() == true)
            {
                System.out.println("You have sadly fallen to the dragon, better luck next time.");
            }
            else
            {
                System.out.println("SOMETHING WENT WRONG!!!");
            }
            break;
        }//end of while loop in.hasNextLine().
    }//end of main
}//end of class
马辛·西姆扎克(Marcin Szymczak)

假设您的attack方法具有与此类似的形式

public void attack(Creature otherCreature){
    otherCreature.decreaseHitPointsBy(this.attackValue);
}

用法看起来像

warrior.attack(dragon);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Java OOP中的NaN输出

来自分类Dev

MATLAB OOP中的对象数组实现

来自分类Dev

如何更新 OOP 类中的对象 json

来自分类Dev

减少在java中的自定义对象操作

来自分类Dev

Java Calendar对象日期操作

来自分类Dev

Perl中的重载对象操作

来自分类Dev

Perl中的重载对象操作

来自分类Dev

对象中的Java对象

来自分类Dev

Java中的OOP原型和继承

来自分类Dev

Java中的OOP原型和继承

来自分类Dev

这在oop java中如何工作?

来自分类Dev

在Java OOP中一次使用2个对象

来自分类Dev

加上对Integer对象的操作,从目录中读取多个文件以在Java中创建词袋

来自分类Dev

OOP设计中仅数据对象可以吗?

来自分类Dev

(伪)C中的OOP从其函数指针获取结构对象

来自分类Dev

Python TypeError:“ int”对象在OOP中不可调用

来自分类Dev

会话中的OOP对象,什么时候不可以?

来自分类Dev

从OOP AS3中的两个对象获取数据

来自分类Dev

什么时候应该在OOP中创建对象?

来自分类Dev

(伪)C中的OOP从其函数指针获取结构对象

来自分类Dev

Java中的指针操作

来自分类Dev

在Java中操作BigIntegers

来自分类Dev

Java中的UnsignedInt操作

来自分类Dev

Java-4个线程在两个同步方法中操作相同的对象数据

来自分类Dev

Java 8 Stream-对Collection中具有相同ID的对象的操作

来自分类Dev

层级对象上的Java Lambda操作

来自分类Dev

Java,使用对象数组进行操作

来自分类Dev

javascript对象中的推入和移入操作

来自分类Dev

如何在AppleScript中操作“数据”对象