简体   繁体   English

跨类实现和维护价值的最佳方式

[英]Best way to implement and maintain value across classes

So, for fun while I'm in my early college I decided it would be good practice to program various projects.所以,在我上大学的时候,为了好玩,我决定对各种项目进行编程是一种很好的做法。 One project I'm working on is basically just a text based game.我正在做的一个项目基本上只是一个基于文本的游戏。 I'm implementing spells and abilities, and I have a string array for my enemies, I'd like to maintain the random value(enemy) that is generated from this code to my abilities class for calculation.我正在执行咒语和能力,我有一个字符串数组供我的敌人使用,我想将此代码生成的随机值(敌人)保存到我的能力 class 以进行计算。 Should I even go with another class for abilities and spells?我是否应该将 go 与另一个 class 一起获得能力和咒语? I'm not entirely sure I'm on the right track to developing proper OOP programming practices and habits, so if anyone can help to point me to the right path, I'd appreciate it.我不完全确定我是否在正确的轨道上开发正确的 OOP 编程实践和习惯,所以如果有人能帮助我指出正确的道路,我将不胜感激。

System.out.println("Welcome to Mysterical World of Mysterical Things!");
System.out.println("Please press the number corresponding the action you wish to take.");   
//Clause to keep the game running with invalid inputs
GAME:


while(running) {
    System.out.println("-----------------------------------------------");
    int enemyHealth = rand.nextInt(maxEnemyHealth);
    String enemy = enemies[rand.nextInt(enemies.length)];
    System.out.println("\t### " + enemy + " has appeared! ###\n");

And the class I'm trying to maintain that string enemy value is:我试图保持字符串敌人值的 class 是:

public void fireBlast(int fb) {

    fb = 5;

    System.out.println("You cast Fire Blast!");
    int damageTaken = rand.nextInt(enemyAttackDmg);
    enemyHealth -= fb*level;
    health -= damageTaken;
    System.out.println("\t>>> You hit the " + enemy + " for " + fb + " damage <<<");
    System.out.println("\t>>> You took " + damageTaken + " <<<");

As a rule of thumb, globals = bad.根据经验,全局变量 = 不好。 Evil Globals 邪恶的全球人

Of course, if you stuck with methods instead of classes, you could just have class variables declared outside main().当然,如果你坚持使用方法而不是类,你可以只在 main() 之外声明 class 变量。

You do have some options though.不过,您确实有一些选择。 You could pass in the String or String[] as an argument in the functions that you want to have access to them.您可以将 String 或 String[] 作为参数传入您想要访问它们的函数中。 So if you have some String[] of enemies, and you want to pass them into a funciton of another class...因此,如果您有一些 String[] 敌人,并且您想将它们传递给另一个 class 的函数...

MiddleEarth me = new MiddleEarth(); // MiddleEarth is a class, me is the instance of that class
me.mordor(enemies); // I have now passed the enemies into the function Mordor of the class MiddleEarth

And now, in the middle earth class, you need mordor() to accept a String or String[] of enemies:现在,在中土 class 中,你需要 mordor() 来接受一个 String 或 String[] 的敌人:

public class MiddleEarth {
  public MiddleEarth() {
    //constructor
  }

  public void mordor(String[] enemies) {
    // do something with enemies
  }
}

The other, less advisable option is to have an interface named Globals and store variables you want accessible from all classes in there, and implement that interface in the classes you want to have access to these global vars.另一个不太可取的选择是拥有一个名为 Globals 的接口并存储您希望从其中的所有类访问的变量,并在您希望访问这些全局变量的类中实现该接口。 I don't recommend this at all.我完全不推荐这个。

Also - why do you pass in int fb to fireBlast() if the first thin you do is set it to 5?另外 - 如果你做的第一个薄设置为 5,你为什么要将int fb传递给 fireBlast()? This defeats the purpose.这违背了目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM