我试图在Java中操纵对象。为了更好地解释,我有一个名为Creature的超类和两个名为(Dragon and Warrior)的子类。我为龙和战士创造了两个对象,每个对象互相争斗。如何设置一个攻击方法,它会对一个对象造成损害,并使用该数字从第二个对象的健康状况中减去它。
请注意,warrior和dragon都是子类。
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
答案 0 :(得分:3)
假设您的attack
方法的格式类似于此
public void attack(Creature otherCreature){
otherCreature.decreaseHitPointsBy(this.attackValue);
}
用法看起来像
warrior.attack(dragon);
答案 1 :(得分:1)
例如,您可以创建两个扩展生物的classess,然后使用new
运算符创建它们的实例。他们可以通过将引用作为攻击方法的参数传递给其他对象来进行交互:
class Creature {
protected int health = 100;
protected int strength = 10;
public void attack(Creature other) {
other.takeDamage(strength);
}
public void takeDamage(int amount) {
health -= amount;
}
}
class Warrior extends Creature {
protected int strength = 2;
//method specific to warrior
public void eatPotion() {
health = 100;
}
}
class Dragon extends Creature {
//You can simply override values in subclasses
protected int strength = 20;
protected int health = 1000;
}
Dragon dragon = new Dragon();
Warrior warrior = new Warrior();
warrior.attack(dragon);
dragon.attack(warrior);
warrior.eatPotion();
warrior.attack(dragon);