无法对非静态对象进行静态引用

时间:2013-08-21 02:32:43

标签: java static non-static

这是我的第一个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!
    }
}

5 个答案:

答案 0 :(得分:0)

townRey不应该延伸结算。您应该在某种方法中将其声明为结算实例,如下所示:

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

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

另外,样式注释:通常,在Java中,类应以大写字母开头,因此结算而不是结算可能会有更好的名称。

答案 1 :(得分:0)

您应该定义一个townRey对象,然后使用此对象来调用returnStrength

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

public class mainclss()
{
public static void main(String arg[])
{
townRey= new settlement();
//you can do sth you like
}  
}

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

答案 4 :(得分:0)

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

public class MainClass {

    public static void main(String[] args) {

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


    }

}

这个工作很好。所以你可以安全地使用它。

注意:您应该通过练习来学习使用大写字母(例如SettlementTownRey)来启动班级名称中的每个单词。祝你好运!