【问题标题】:Cannot make static reference to non-static object无法对非静态对象进行静态引用
【发布时间】:2013-08-21 02:32:42
【问题描述】:

这是我的第一个 Java 项目。

所以我正在开发自己的模拟项目,但我的一些核心内容出现了问题。我现在专注于两个课程 - 定居点和 townRey,它扩展了定居点。

当我尝试时抛出错误

System.out.println(townRey.returnStrength());

这是我的两个相关课程:

结算:

public class settlement
{
    //
    //
    // VARIABLES
    //
    //

    /**
     * The town's unique name.
     */
    public String name;

    /**
     * The settlement's location in latitude (N-S)
     */
    public int latitude;

    /**
     * The settlement's location in longitude (E-W)
     */
    public int longitude;

    /**
     * What faction a town or village is aligned to. This determines production and consumption, mostly.
     */
    public String faction;

    /**
     * What a specific village or town produces.
     */
    public String[] production;

    /**
     * What a specific town consumes (villages don't consume)
     */
    public String[] consumption;

    /**
     * How dangerous a specific town is with bandits
     * A 1-10 scale, with 10 being the most dangerous.
     * Any town with a danger over 8 can be raided and destroyed temporarily by bandits.
     * Being raided successfully depends on the Strength of a town.
     */
    public int danger;

    /**
     * How much a town takes in taxes.
     */
    public float tax;

    /**
     * How easily a town is raided by bandits.
     * If a bandit raid has a lower strength than the town, then the town wins.
     */
    public int strength;

    //
    //
    // METHODS
    //
    //

    public int returnLatitude() 
    {
        return latitude;
    }

    public int returnLongitude()
    {
        return longitude;
    }

    public String returnFaction()
    {
        return faction;
    }

    public String[] returnProduction()
    {
        return production;
    }

    public String[] returnConsumption()
    {
        return consumption;
    }

    public int returnDanger()
    {
        return danger;
    }

    public float returnTax()
    {
        return tax;
    }

    public int returnStrength()
    {
        return strength;
    }
}

和townRey:

public class townRey extends settlement
{{
    name = "Rey";
    latitude = 5;
    longitude = 5;
    String faction = "Nord";
    String[] production;
    String[] consumption;
    danger = 1;
    tax = 0.05F;
    strength = 6;
}}

编辑:: 感谢所有帮助!我现在解决了所有问题。下面是“结算”和“开始”。

public class Settlement
{
    //
    //
    // VARIABLES
    //
    //

    /**
     * The town's unique name.
     */
    public String name;

    /**
     * The settlement's location in latitude (N-S)
     */
    public int latitude;

    /**
     * The settlement's location in longitude (E-W)
     */
    public int longitude;

    /**
     * What faction a town or village is aligned to. This determines production and consumption, mostly.
     */
    public String faction;

    /**
     * What a specific village or town produces.
     */
    public String[] production;

    /**
     * What a specific town consumes (villages don't consume)
     */
    public String[] consumption;

    /**
     * How dangerous a specific town is with bandits
     * A 1-10 scale, with 10 being the most dangerous.
     * Any town with a danger over 8 can be raided and destroyed temporarily by bandits.
     * Being raided successfully depends on the Strength of a town.
     */
    public int danger;

    /**
     * How much a town takes in taxes.
     */
    public float tax;

    /**
     * How easily a town is raided by bandits.
     * If a bandit raid has a lower strength than the town, then the town wins.
     */
    public int strength;

    //
    //
    // METHODS
    //
    //

    public int returnLatitude() 
    {
        return latitude;
    }

    public int returnLongitude()
    {
        return longitude;
    }

    public String returnFaction()
    {
        return faction;
    }

    public String[] returnProduction()
    {
        return production;
    }

    public String[] returnConsumption()
    {
        return consumption;
    }

    public int returnDanger()
    {
        return danger;
    }

    public float returnTax()
    {
        return tax;
    }

    public int returnStrength()
    {
        return strength;
    }
}

然后开始,我在这里创建“townRey”,然后以两种不同的方式访问一些数据。

public class Start 
{
    public static void main(String[] args) 
    {
        //Creates 'Rey'
        Settlement townRey = new Settlement();
        townRey.name = "Rey";
        townRey.latitude = 5;
        townRey.longitude = 5;
        townRey.faction = "Nord";
        townRey.danger = 1;
        townRey.tax = 0.05F;
        townRey.strength = 6;

        //This calls the returnLongitude method from Settlement, and is the 'proper' way to do it.
        System.out.println(townRey.returnLongitude());

        //This also works.
        System.out.println(townRey.longitude);

        //Thanks for the help!
    }
}

【问题讨论】:

  • “这是我的第一个 Java 项目。” - 请不要以小写字母开头的 Java 类名。永远不会!立即学习这一课。
  • 虽然我经常听到这句话并接受它,因为它是流行的风格,但我很想知道为什么这个话题会引起人们如此强烈的愤怒。例如,让类以小写字母开头而变量以大写字母开头的标准会有什么问题?
  • @qaphla 如果那是标准的话,那也没什么问题。该标准是任意的,但普遍存在,使用错误的大小写就像拼写错误:读者通常可以理解您的意思,但需要更多的努力才能更好地解决实际问题。
  • 我不确定这是一个糟糕的复制粘贴还是 OP 没有提到的错误,但 townRey 的当前代码(带有双括号 {{)在初始化块中包含所有内容,考虑到数组声明,这几乎肯定不是预期的。
  • 我会强烈建议从 Java 初学者书籍或 Oracle 教程开始。随意在您的计算机中输入内容并期望它们通常能够正常工作不会产生您想要的结果。

标签: java static non-static


【解决方案1】:

townRey 不应该扩大定居点。您应该以某种方法将其声明为结算实例,如下所示:

townRey = new settlement();
townRey.name = "Rey";
...
townRey.strength = 6;

或者,更好的是,为结算创建一个新的构造函数,将不同的字段作为输入。

另外,风格说明:通常,在 Java 中,类应该以大写字母开头,因此 Settlement 而不是 Settlement 可能是一个更好的名称。

【讨论】:

    【解决方案2】:

    你应该定义一个townRey对象然后用这个对象调用returnStrength

    townRey mytownRey = new townRey();
    System.out.println(townRey.returnStrength());
    

    【讨论】:

      【解决方案3】:

      我希望您希望 townRey 成为 settlement 的实例,而不是子类。除非您想拥有多个 townRey 副本。将public class townRey extends settlement 行替换为settlement townRey = new settlement(),并在}} 之后添加分号。其他一切都保持不变。

      【讨论】:

        【解决方案4】:
        public class mainclss()
        {
        public static void main(String arg[])
        {
        townRey= new settlement();
        //you can do sth you like
        }  
        }
        

        创建一个新的类来检查。不要用 Class 启动 Java!这有点困难。

        【讨论】:

          【解决方案5】:

          使用main() 方法创建一个单独的类。在此方法中,您应该创建一个 townRey 对象,以便访问方法 returnStrength()。如果您这样做,则无法使用类名“townRay”访问它。所以用下面的代码添加这个类:

          public class MainClass {
          
              public static void main(String[] args) {
          
                  townRey  tr = new townRey();
                  System.out.println( tr.returnStrength () );
          
          
              }
          
          }
          

          工作对我来说很好。这样您就可以放心使用了。

          注意:您应该通过练习学会在班级名称中的每个单词都以大写字母开头,例如SettlementTownRey。祝你好运!

          【讨论】:

            猜你喜欢
            • 2011-06-25
            • 2021-08-31
            • 1970-01-01
            • 2023-04-02
            相关资源
            最近更新 更多