gpt4 book ai didi

java - 单独的类/方法中的方法实例?

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:07 25 4
gpt4 key购买 nike

我正在创建一个最终幻想角色的小游戏,在其中输入我想要“战斗”的人的名字。我将每个角色(只有 5 个)作为名为 Stats 的父类(super class)的子类,其中定义了变量(非静态)和 getter/setter。

代码都按照我想要的方式工作,但我不喜欢它们都在一个巨大的类中。

Main方法在这里;

package com.George.revision;

import java.util.Random;

import com.George.characters.Cloud;
import com.George.characters.Squall;
import com.George.characters.Stats;
import com.George.characters.TheEnemy;
import com.George.characters.ThePlayer;
import com.George.characters.Tidus;
import com.George.characters.Yuna;
import com.George.characters.Zidane;
import com.George.input.GetInput;
import com.George.input.ListNames;



public class Main {


public static void main(String[] args) {

ListNames.listNames();


Stats clo = new Cloud();
Stats squ = new Squall();
Stats zid = new Zidane();
Stats tid = new Tidus();
Stats yun = new Yuna();

String versus = GetInput.getInput("\nWhich of these Characters would you like to go up against?");

Stats ene1 = new TheEnemy();


switch (versus) {
case "Cloud":
ene1.setName(Names.CLOUD);
ene1.setHairColor(Stats.BLONDE);
ene1.setWep(Weapons.BUSTER_SWORD);
ene1.setSkill(clo.skill);
ene1.setAp(clo.ap);
ene1.setStr(clo.str);
ene1.setMag(clo.mag);
break;
case "Squall":
ene1.setName(Names.SQUALL);
ene1.setHairColor(Stats.BLACK);
ene1.setWep(Weapons.LIONHEART);
ene1.setSkill(squ.skill);
ene1.setAp(squ.ap);
ene1.setStr(squ.str);
ene1.setMag(squ.mag);
break;
case "Zidane":
ene1.setName(Names.ZIDANE);
ene1.setHairColor(Stats.LIGHTBROWN);
ene1.setWep(Weapons.THIEF_DAGGER);
ene1.setSkill(zid.skill);
ene1.setAp(zid.ap);
ene1.setStr(zid.str);
ene1.setMag(zid.mag);
break;
case "Tidus":
ene1.setName(Names.TIDUS);
ene1.setHairColor(Stats.BLONDE);
ene1.setWep(Weapons.CALADBOLG);
ene1.setSkill(tid.skill);
ene1.setAp(tid.ap);
ene1.setStr(tid.str);
ene1.setMag(tid.mag);
break;
case "Yuna":
ene1.setName(Names.YUNA);
ene1.setHairColor(Stats.DARKBROWN);
ene1.setWep(Weapons.NIRVANA);
ene1.setSkill(yun.skill);
ene1.setAp(yun.ap);
ene1.setStr(yun.str);
ene1.setMag(yun.mag);
break;
default:
System.out.println("You did not enter a valid character name");
break;


}
System.out.println("You have chosen to face " + ene1.name);
System.out.println("Enemy Skill = " + ene1.skill + " Enemy Weapon = " + ene1.wep);



System.out.println("Enemy Skill = " + ene1.skill + " Enemy Weapon = " + ene1.wep);

int eneTotal = ene1.skill + ene1.ap + ene1.str + ene1.mag;

Stats player = new ThePlayer();

String plN = GetInput.getInput("What is your name?");
player.playerName = plN;
System.out.println("So Your name is " + player.playerName);

String plWep = GetInput.getInput("What is your Weapon's name?");
player.playerWep = plWep;
System.out.println("So your Weapon is " + player.playerWep);

Random generator = new Random();
int plSkill = generator.nextInt(10);
player.skill = plSkill;
System.out.println("Your skill level is " + player.skill);

Random gn = new Random();
int plAp = gn.nextInt(10 - 5) + 5;
System.out.println("So your Attack Power is " + plAp);
player.ap = plAp;

Random gns = new Random();
int plStr = gns.nextInt(10);
System.out.println("So your Strength is " + plStr);
plStr = player.str;

Random gnm = new Random();
int plMag = gnm.nextInt(10 - 5) + 5;
player.mag = plMag;
System.out.println("So your Magic is " + player.mag);


int plHax = 15;
double doubleResult = plHax;


double ene1Hax = 3.99;
int intResult = (int)ene1Hax;


double doubleValue = 6.99;
Double doubleObj = new Double(doubleValue);
int intR = doubleObj.intValue();

System.out.println(intR);

int plyrTotal = player.skill + player.ap + player.str + player.mag;

if (plyrTotal > eneTotal) {
System.out.println("Congratulations you beat " + ene1.name + " Please Play Again!" );

} else if (plyrTotal == eneTotal) {
System.out.println("You drew with " + ene1.name + " Play again and Win!");
}
else
{
System.out.println("You were not succesful this time " + ene1.name + " Defeated you by " + (eneTotal - plyrTotal) + " Please Try Again");

}


}

}

现在,在此之后,我有更多的代码为玩家的“统计数据”和角色生成随机数,然后匹配他们的统计数据的总值以确定“获胜者”,我想将其放入单独的类中。我的问题是,

如何使用主类中 switch 语句中输入的值在单独的类中获取 ene1。

为了清晰起见,更新为完整的主方法

最佳答案

您遇到的问题之一是您将敌人指定为统计数据的子类,但与您的角色无关。因此,虽然敌人具有与角色相同的属性,但它与角色没有任何关系。不要复制所有这些值(例如名称和颜色),而是让敌人成为拥有统计值的自己的实体,并与统计数据共享接口(interface)。然后,敌人可以使用该接口(interface)来调用 Stats 类中的各种方法。

public class Static implements Actions{
// ...
}

public class Enemy implements Actions{
private Static characterType;
}

另外,为了供将来引用,您能否将您的问题设计得更通用?它不仅可以帮助那些不理解引用文献的人,也可以让有类似问题的人更容易找到它。

关于java - 单独的类/方法中的方法实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com