3

我对编程非常陌生,并且对在我认为被称为“嵌套类”的变量中使用变量有疑问。

class BeginningGameTest {

    int attack;
    int defend;

    public static class James
    {
        attack = 25;
        defend = 15;
    }

    public static class Janet
    {
        attack = 45;
        defend = 1;
    }
    public static class Jackson
    {
        attack = 10;
        defend = 20;
    }


    public static void main(String[] args) {

        System.out.prinln(James.attack);

    }
}

我有大致的想法吗?我想保存“相同”的变量,但因类而异,并且在打印行中的访问方式也不同。我确实遇到了一些错误,我应该怎么做才能保持相同的概念并且仍然保持相当简单以便我可以理解它?对于一般的编程新手,是否有任何易于理解的教程?

提前致谢!

4

6 回答 6

3

这样的设计似乎不正确。

当您使用面向对象的语言工作时,您要尝试的是您希望表示的事物的基本模型。

这三个静态类似乎代表相同类型的对象,所以让我们为它们创建一个简单的模型。把模型想象成一个千篇一律的人。每个用它切割的饼干都是相同的通用“形状”,但会有不同的特征(洒水、糖霜胡须等)。这个模型应该在它自己的单独文件中。

public class Player {

    private String name;
    private int attack;
    private int defense;

    public Player(String theirName, int theirAttack, int theirDefense) {
        name = theirName;
        attack = theirAttack;
        defense = theirDefense;
    }

    // create getters and setters for their attack and defense
}

要实际使用它,您需要实例化该对象。

public class BeginningGameTest {
    public static void main(String[] args) {
        Player p1 = new Player("James", 25, 15);
        Player p2 = new Player("Janet", 45, 1);
        Player p3 = new Player("Jackson", 10, 20);
        // interactions with the objects below
    }
}

Java 标签 wiki中已经存在一些极好的初学者资源;给那些一个彻底的阅读。尝试新事物,不要害怕就你不理解的事物提出(好的)问题。

于 2013-02-10T00:48:16.807 回答
2

您应该创建一个内部类,然后在 main 方法中定义该类的实例。

public class BeginningGameTest {


    public static void main(String[] args) {
        Player james = new Player(25,15);
        Player janet = new Player(45,1);
        Player jackson = new Player(10,20);

        System.out.println(james.getAttack());
    }
}

class Player{
    int attack;
    int defend;

    public Player(int attack, int defend){
        this.attack = attack;
        this.defend = defend;
    }

    public int getAttack() {
        return attack;
    }

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

    public int getDefend() {
        return defend;
    }

    public void setDefend(int defend) {
        this.defend = defend;
    }

}
于 2013-02-10T00:36:08.523 回答
2

您应该使用实例的概念来区分人,而不是为每个人定义一个类。您可以定义单个类“Person”并实例化 James、Jackson 等。要给它们每个不同的攻击/防御值,您可以使用带参数的构造函数。

我觉得您可能会从阅读面向对象编程的介绍中受益。尝试搜索“面向对象编程”。

于 2013-02-10T00:38:39.183 回答
1

你可以采取两种方式来解决这个问题。您可以创建子类,使 James、Janet 和 Jackson 都是相同类型的类,即BeginningGameTest。例如,詹姆斯可能是:

public class James extends BeginningGameTest
{
    public James()
    {
        attack = 25;
        defend = 15;
    }
}

我认为您希望 James、Janet 和 Jackson 成为的不是子类,而是同一类BeginningGameTest 的实例,如下所示:

BeginningGameTest James = new BeginningGameTest();
James.setAttack(25);
James.setDefend(15);

您应该阅读一些概念:

  1. 类与实例
  2. 遗产

我还含蓄地向您介绍了 Java bean 典型的 setter(和 getter)的概念。

于 2013-02-10T00:40:58.110 回答
0

这将起作用:

public static class James
{
    static int attack = 25;
    static int defend = 15;
}
// ...

那么这将起作用:

public static void main(String[] args)
{
    System.out.prinln(James.attack);
}

这可能是一个更好的设计:

public class Player()
{
   public static enum NAME { JAMES, JANET };

   int attack, defend;
   public Player(NAME name)
   {
      switch (name)
      {
         case JAMES:
            attack = 25;
            defend = 15;
            break;
         // ...
      }
   }

   public static void main(String[] args) throws Exception
   {
      System.out.println(new Player(NAME.JAMES).attack);
   }
}

对于现实需求,这是一个更好的设计:(允许运行时创建播放器)

int attack, defend;
String name;
public Player(int attack1, int defend1, String name1)
{
   attack = attack1;
   defend = defend1;
   name = name1;
}
于 2013-02-10T00:40:59.223 回答
0

您可以简单地做的是创建您的类的不同对象,这些对象将持有不同值的变量攻击和防御。这是相同的代码。

/* 打包任何东西;// 不要放置包名!*/

class Main
{
    int attack,defend;
    public Main(int attack,int defend)
    {
        this.attack=attack;
        this.defend=defend;
    }
    public void show()
    {
        System.out.println("attack: "
        +attack+" defend: "+defend);
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        Ideone James = new Main(125,15);
        James.show();
        Ideone Janet = new Main(45,1);
        Janet.show();
        Ideone Jackson = new Main(10,20);
        Jackson.show();
    }
}
于 2014-01-21T07:59:54.237 回答