-2

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

5 回答 5

0

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

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

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

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

于 2013-08-21T02:36:45.937 回答
0

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

townRey mytownRey = new townRey();
System.out.println(townRey.returnStrength());
于 2013-08-21T02:36:46.887 回答
0

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

于 2013-08-21T02:42:34.243 回答
0
public class mainclss()
{
public static void main(String arg[])
{
townRey= new settlement();
//you can do sth you like
}  
}

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

于 2013-08-21T02:44:33.857 回答
0

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

public class MainClass {

    public static void main(String[] args) {

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


    }

}

这对我来说很好。因此,您可以安全地使用它。

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

于 2013-08-21T02:48:31.920 回答