0

基本クラスが1つあります。

class Tile{}

そして、タイルを拡張する他のいくつか

class Free : Tile{}
class Wall : Tile{}

各タイルには独自のテクスチャがあり、文字列ではなく、初期化時にロードする必要があるTexture2Dです。コードはこれに似ていると思いますが、これを適切に作成する方法がわかりません。

class Tile{
    static Texture2D texture; //Static will use less ram because it will be same for inherited class?
    static string texture_path; //This is set by inherited class
    public Tile(){
        if(texture==null)
            texture = LoadTexture(texture_path);
    }
}

class Free : Tile{
    static string texture_path = "Content/wall.png";
}

言い換えれば、すべてのフリータイルは同じテクスチャを持ち、すべてのウォールタイルは同じテクスチャを持っています。そのため、私の意見では静的を使用する必要があります。

これを正しく行う方法は?

4

3 に答える 3

0

あなたの質問によると、のFreeすべてのインスタンスがテクスチャを共有し、のすべてのインスタンスがテクスチャをWall共有するようにします。これは、staticフィールドが必要でtextureありtexture_path、親ではなく子クラスに含まれることを意味します。

元:

public class Tile { }

public class Free : Tile
{
    private static Texture2D texture;
    private static string texture_path;
}

public class Wall : Tile
{
    private static Texture2D texture;
    private static string texture_path;
}

共有またはインスタンスからアクセスできるように、Tile参照にプロパティを持たtextureせたい場合は、またはプロパティが必要です。texture_pathtexturetexture_pathvirtualabstract

元:

public abstract class Tile
{
    public abstract Texture2D Texture { get; }
    public abstract string TexturePath { get; }
}

public class Free : Tile
{
   private static Texture2D texture;
   private static string texture_path;

   public override Texture2D Texture { get { return texture; } }
   public override string TexturePath { get { return texture_path; } }
}

// and similarly for Wall
于 2012-11-04T05:47:38.613 に答える
0

基本クラスで texture_path にアクセスできるようにする場合は、基本クラスで宣言する必要があります。

基本クラスは、そのサブクラスで宣言されたフィールド、プロパティ、またはメソッドについて何も知りません。これは設計によるものです...

于 2012-11-03T14:09:39.497 に答える