2013-05-20 217 views
0

所以我对编程一般都很陌生。我目前工作的一个地形生成程序,一切都会好起来,除了这个伟大:有没有办法让这个缩短?

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

编辑:这可能是明智的告诉你,我永远不会改变类内的东西,除了'机会'的价值的例外。

+1

删除“静态”的关键字,并把所有在构造 – MikeSW

+0

@AwesomePerson怎么样的试图在“列表”中使用此类 – Praveen

+0

您正在将类与其实例混淆。不要声明7个类,声明1并为每个生物实例化它。然后出去买一本关于编程概念的书吧:-) –

回答

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

有了这一点,很难看到哪个参数去哪个领域,但它的很多短。

如果这些字段必须是只读的,那么您可以使用公共getset访问器创建属性。例如:

public Chance { get { return chance; } } 

在这种情况下做出的领域专用:

private int chance = iChance; 

(就个人而言,对于这样的场景,我会把所有的数据文件)

0

这是更好地使用只有一个类没有继承,甚至没有结构。沙漠,草地等不是逻辑上的类,它必须是对象(可能是常量)。

-2

首先,你不能在static classes上继承。所以你将不得不开始使用实例。

其次,如果你想扩展对象,你会使用继承。例如,如果你想添加一个新的属性“bool HasScorpions”到沙漠上,但不是在草地上。

由于您使用相同的属性,但想要使用不同的值我个人使用一个接口。通过这种方式,您可以只读属性,同时仍然可以轻松设置值。

public interface Terrain 
{ 
    int iChance = {get { return 15; private set; } ..and repeat. 
    int chance = iChance; 
    int chancepoint = 0; 
    int octaves = 4; 
    int lengthMin = 60; 
    int lengthMax = 90; 
    float scaleMin = 250; 
    float scaleMax = 350; 
    float persistenceMin = 0.5f; 
    float persistenceMax = 0.9f; 
    pType ptype = pType.Lowland; 
    bTag[] tags = { bTag.desert }; 
} 
1

以下将更短:

public const int iChance = 15, octaves = 4, lengthMin = 60, lengthMax = 90; 
public const float scaleMin = 250, scaleMax = 350, persistenceMin = 0.5f, 
        persistenceMax = 0.9f; 
public static int chance = iChance, chancepoint = 0; 

但是......这些真的不喜欢看的东西,应该是static领域,或很可能不连const。它们看起来应该是实例属性。也许是这样的:

public class Terrain { 
    public int Chance {get;private set;} 
    public int LengthMin {get;private set;} 
    // ... 
    private Terrain(int chance, int lengthMin, ...) { 
     Chance = chance; 
     LengthMin = lengthMin; 
     // ... 
    } 
    private static readonly Terrain 
     desert = new Terrain(45, 45, ...), 
     meadow = new Terrain(15, 60, ...), 
     ...; 
    public static Terrain Desert { get { return desert;}} 
    public static Terrain Meadow { get { return meadow;}} 
} 
+0

好的,这是有道理的,但一旦我创造它们并且表现相当重要,我就永远不会改变班上的东西。这会影响性能吗? – AwesomePerson

+0

@AwesomePerson棘手的一个。常量的优点是它们被“烧入”调用者(它绝不是ldfld等 - 它是一个原始的加载值)。这种情况有多重要取决于情况(您需要进行配置)。但是这里的第一个示例中的语法(每个声明有多个值)至少不会太冗长! –

+0

好吧,谢谢! – AwesomePerson

0

你可以做的是使用静态构造函数中使用一个单独的类名为地形和初始化这个多次:

public class Terrain 
    { 
      public int IChance { get; private set; } 
    public int Chancepoint { get; private set; } 
    public int Octaves { get; private set; } 
    public int LengthMin { get; private set; } 
    public int LengthMax { get; private set; } 
    public float ScaleMin { get; private set; } 
    public float ScaleMax { get; private set; } 
    public float PersistenceMin { get; private set; } 
    public float PersistenceMax { get; private set; } 
    public pType Ptype { get; private set; } 
    public bTag[] Tags { get; private set; } 

     public static Terrain Desert() 
     { 
      return new Terrain 
       { 
        IChance = 15, 
        Chancepoint = 0, 
        Octaves = 4, 
        LengthMin = 60, 
        LengthMax = 90, 
        ScaleMin = 250, 
        ScaleMax = 350, 
        PersistenceMin = 0.5f, 
        PersistenceMax = 0.9f, 
        Ptype = pType.Lowland, 
        Tags = new bTag[] {bTag.Desert} 
       }; 
     } 
} 
0

乔的回答是好,但构造函数调用已远太多未命名的参数 - 350是什么意思?

这是数据驱动设计的理想人选。

与其定义代码中的所有Biome类型,不如将Biome类型的所有数据放入文件中,并在运行时读取文件。 C#语言有很多东西可以帮助你做到这一点,关键词搜索是序列化(and here's a link to MSDN about it)。

最大的优点是您可以更改数据值而无需重新编译代码。

缺点是需要更多的代码来定义第一个实例,但在此之后,您可以根据需要轻松创建多个实例。

1

我对地形生成程序了解不多,但应该将数据存储在数据库中。 然后创建类以将该数据映射到您的应用程序。 我建议你查找“数据结构”并查看哪一个最适合你的应用。

0

你可以做这样的事情,宣布这样一个抽象类,然后从它inherting:

public abstract class Terrain 
    { 
     public int iChance; 
     public int chance; 
     public int chancepoint; 
     public int octaves; 
     public int lengthMin; 
     public int lengthMax; 
     public float scaleMin; 
     public float scaleMax; 
     public float persistenceMin; 
     public float persistenceMax; 
     public pType ptype; 
     public Tag[] strongTags; 
    } 

    public class Desert : Terrain 
    { 

    } 

    public enum pType 
    { 
     Desert = 1, 
     LowLand = 2 
    } 

    public enum Tag 
    { 
     desert = 1, 
     lush = 2 
    } 

然后,您可以实例沙漠像:

var desert = new Desert() 
      { 
       iChance = 15 
       ,chance = 15 
       ,chancepoint = 0 
       ,octaves = 4 
       ,lengthMin = 60 
       ,lengthMax = 90 
       ,scaleMin = 250 
       ,scaleMax = 350 
       ,persistenceMin = 0.5f 
       ,persistenceMax = 0.9f 
       ,ptype = pType.Desert 
       ,strongTags = new Tag[]{Tag.desert} 
      }; 
0

不知道你的具体要求,但这不是一个更好的办法:

public abstract class BiomeBase 
{ 
    public int Chance  { get; set; } 
    public int Chancepoint { get; set; } 
    public int Octaves  { get; set; } 
    // you get the idea ... 
} 

那么你有DesertMeadow继承:

public class Desert : BiomeBase 
{ 
    // everything is inherited ... 
    // you can also add your own properties meant for Desert only (if needed) 
} 

public class Meadow : BiomeBase 
{ 
    // everything is inherited ... 
} 

现在Desert拥有一切Biome有,你可以使用它像这样:

var desert = new Desert 
{ 
    Chance = 5, 
    Octaves = 1, 
    /// etc 
}; 
+0

冬青牛......多少个答案......我什至不:))))对不起,如果重复。 –

相关问题