-1

だから私は一般的にプログラミングにかなり慣れていません。私は現在、地形生成プログラムに取り組んでいます。これを除いて、すべてが順調に進んでいます。

    public static class Desert
    {
        public const int iChance = 15;
        public static int chance = iChance;
        public static int chancepoint = 0;
        public const int octaves = 4;
        public const int lengthMin = 60;
        public const int lengthMax = 90;
        public const float scaleMin = 250;
        public const float scaleMax = 350;
        public const float persistenceMin = 0.5f;
        public const float persistenceMax = 0.9f;
        public const pType ptype = pType.Lowland;
        public const bTag[] tags = { bTag.desert };
    }
    public static class Meadow
    {
        public const int iChance = 45;
        public static int chance = iChance;
        public static int chancepoint = 0;
        public const int octaves = 4;
        public const int lengthMin = 45;
        public const int lengthMax = 70;
        public const float scaleMin = 200;
        public const float scaleMax = 470;
        public const float persistenceMin = 0.35f;
        public const float persistenceMax = 0.70f;
        public const pType ptype = pType.noAbs;
        public const bTag[] tags = { bTag.lush };
    }

これらは、「バイオーム」の異なるタイプごとのプロパティです。

私は現在これらのうち約7つを持っていますが、各フィールドの値を除いてすべてまったく同じです.

コードを短くする方法はありますか? 継承を調べましたが、エラーになってしまい、少し混乱しました。><

私が書かなければならなかったのは素晴らしいことです:

public static class Desert
    {
        iChance = 15;
        chance = iChance;
        chancepoint = 0;
        octaves = 4;
        lengthMin = 60;
        lengthMax = 90;
        scaleMin = 250;
        scaleMax = 350;
        persistenceMin = 0.5f;
        persistenceMax = 0.9f;
        ptype = pType.Lowland;
        strongTags = { bTag.desert };
    }

前もって感謝します。

ああ、質問が曖昧で申し訳ありません。プログラムの残りの部分を見たら、私のコードがいかにひどいものであるかと叫ぶことでしょう。XD

編集:「チャンス」の値を除いて、クラス内のものを二度と変更しないことを伝えるのはおそらく賢明です。

4

9 に答える 9

3

静的クラスを使用する代わりに、非静的クラスを使用できます。

public class Biome {
    // Instance fields with default values
    public int iChance = 15;
    public int chance = iChance;
    public int chancepoint = 0;
    public int octaves = 4;
    public int lengthMin = 60;
    public int lengthMax = 90;
    public float scaleMin = 250;
    public float scaleMax = 350;
    public float persistenceMin = 0.5f;
    public float persistenceMax = 0.9f;
    public pType ptype = pType.Lowland;
    public bTag[] tags = { bTag.desert };
}

ここでは、初期化にコンストラクターを使用します。

public Biome(int iChance, int chance, int chancepoint, int octaves, public int lengthMin, int lengthMax, float scaleMin, float scaleMax, float persistenceMin, float persistenceMax,pType ptype, bTag[] tags) {
    // init fields here
}

次に、コンストラクターを呼び出します。

Biome bimoe = new Biome(15, iChance, 0, 4, 60, 90, 250, 350, 0.5f, 0.9f, pType.Lowland, { bTag.desert });

これにより、どのパラメーターがどのフィールドに移動するかを確認するのは困難ですが、はるかに短くなります。

フィールドを読み取り専用にする必要がある場合は、パブリックのみでアクセサーgetを持たないプロパティを作成できます。set例:

public Chance { get { return chance; } }

この場合、フィールドを非公開にします。

private int chance = iChance;

(個人的には、そのようなシナリオでは、すべてのデータをファイルに入れます)

于 2013-05-20T08:19:04.970 に答える