【问题标题】:Using variables in a nested class JAVA在嵌套类 JAVA 中使用变量
【发布时间】:2013-02-10 00:30:35
【问题描述】:

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

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);

    }
}

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

提前致谢!

【问题讨论】:

  • 什么是编译器错误?
  • 为什么不编译并运行您的代码,看看它是否符合您的预期?然后,如果它不符合您的期望,您可以阅读tutorial
  • 就像我说的,我是个新手。有什么地方可以让我了解一下课程的运作方式吗?
  • @user2057930 从谷歌开始。那里有 很多 的基本 Java 教程。来自 Sun/Oracle 的官方版本是一个很好的起点。

标签: java class variables nested


【解决方案1】:

这个设计似乎不正确。

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

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

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 tag wiki中已经存在一些极好的初学者资源;给那些一个彻底的阅读。尝试新事物,不要害怕就你不理解的事物提出(好的)问题。

【讨论】:

    【解决方案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;
        }
    
    }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        您可以通过两种方式解决此问题。您可以创建子类,使 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)的概念。

        【讨论】:

          【解决方案5】:

          这将起作用:

          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;
          }
          

          【讨论】:

          • ...如果我想要一个名为“Makoto”的玩家怎么办?
          • 所述玩家可能不诚实并且谎报他/她的攻击/防守(但有权调用该函数?),我们可能希望避免这种情况。或者诚根本不被允许玩。这一切都取决于要求。我的最后一次编辑可能对实际需求最有意义。
          【解决方案6】:

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

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

          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();
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2015-07-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-26
            • 1970-01-01
            • 2015-04-17
            • 2014-08-24
            • 1970-01-01
            相关资源
            最近更新 更多