-2

これは私の最初の Java プロジェクトです。

それで、私は自分のシミュレーション プロジェクトに取り組んでいますが、私の中心的なもののいくつかが失敗してしまいました。私が今注目している 2 つのクラスがあります。決済と、決済を拡張する townRey です。

試してみるとエラーがスローされます

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

ここに私の2つの関連するクラスがあります:

決済:

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

とタウンレイ:

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」を作成し、2 つの異なる方法でデータにアクセスします。

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 = new settlement();
townRey.name = "Rey";
...
townRey.strength = 6;

または、さらに良いのは、さまざまなフィールドを入力として受け取る決済用の新しいコンストラクターを作成することです。

また、スタイル ノート: 一般に、Java では、クラスは大文字で始まる必要があるため、和解よりも和解の方が適切な名前になる可能性があります。

于 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の複数のコピーが必要な場合を除きますtownReypublic class townRey extends settlement行をに置き換え、 のsettlement townRey = new settlement()後にセミコロンを追加し}}ます。他のすべてを同じままにします。

于 2013-08-21T02:42:34.243 に答える