2009-11-21 44 views
5

Vấn đề của tôi một cách ngắn gọn:C#: Thừa hưởng các thành viên tĩnh riêng cho các lớp thừa kế

class A 
{ 
    /* Other stuff in my class*/ 
    protected static staticMember; 
} 

class B : A 
{ 
    /* Other stuff in my class*/ 
    // Will have A.staticMember but I want B.staticMember (same type) 
} 

class C : A 
{ 
    /* Other stuff in my class*/ 
    // Will have A.staticMember but I want C.staticMember (same type) 
} 

Vì vậy, tôi muốn tất cả các lớp được thừa kế của tôi để có một chia sẻ dữ liệu được chia sẻ cho rằng lớp paricular nhưng có một chữ ký thông thường quy định tại lớp cơ sở.

Tôi đang tạo một trò chơi RTS đơn giản để giải trí trong thời gian rảnh rỗi của mình. Có một số loại đơn vị (tàu vũ trụ, tòa nhà, v.v.) có một số thuộc tính cơ bản. Các đơn vị này có thể được nâng cấp bởi người chơi (tất cả các đơn vị cùng loại thuộc cùng một người chơi đều được nâng cấp ví dụ: Người chơi A nâng cấp giáp của xe tăng có nghĩa là tất cả xe tăng của mình sẽ có giáp tốt hơn.)

Đây là làm thế nào tôi đã cố gắng để đạt được điều này:

abstract class Unit 
{ 
    /*several methods that is common for all units*/ 

    /*We don't know the unit's attributes at this point*/ 
    protected abstract double getMaxHitpoints(); 
    protected abstract double getFusionArmor(); 
    protected abstract double getNormalArmor(); 
    // and more similar abstract methods to come. 

    double maxHitpoints; 
    double fusionArmor; 
    double normalArmor; 
    //... 

    // This method is called on construction and after upgrade completion. 
    public void cacheAttributes(Player forPlayer) 
    { 
     Upgrade upgradesForThisUnit; //<<< Upgrade is class that is used like a struct to hold the upgrades for this kind of unit. 
     upgrades.TryGetValue(forPlayer,out upgradesForThisUnit); ///< get upgrades if available (if not available it will give the default value [=no bonuses]) 

     maxHitpoints=getMaxHitpoints()+upgradesForThisUnit.hitpointBonus; 
     fusionArmor=getFusionArmor()+upgradesForThisUnit.fusionArmorBonus; 
     normalArmor=getNormalArmor()+upgradesForThisUnit.normalArmorBonus; 
     //... 
    } 
    // This data structure is intended to hold the upgrades for every player for this kind of the unit 
    // but unfortunally derived classes have this instance too so if the player upgrades the tanks it will upgrade the interceptors, peasants, buildings too... 

    protected static Dictionary<Player,Upgrade> upgrades; 
} 

class Tank : Unit 
{ 
    protected override double getMaxHitpoints() {return 1000;} 
    protected override double getFusionArmor() {return 10;} 
    protected override double getNormalArmor() {return 50;} 
    //... 
} 

tôi nghĩ về việc thêm một chìa khóa khác để dictonary của tôi (sử dụng từ điển lồng nhau): cấu trúc Kiểu như chìa khóa và sửa đổi các mã như thế này:

protected static Dictionary<Player,Dictionary<Type,Upgrade>> upgrades; 

public void cacheAttributes(Player forPlayer) 
{ 
    Dictionary<Type,Upgrade> upgradesForThePlayer; 
    upgrades.TryGetValue(forPlayer,out upgradesForThePlayer); 

    Upgrade upgradesForThisUnit; //<<< Upgrade is class that is used like a struct to hold the upgrades for this kind of unit. 
    upgradesForThePlayer.TryGetValue(GetType(),out upgradesForThisUnit); ///< get upgrades if available (if not available it will give the default value [=no bonuses]) 

    maxHitpoints=getMaxHitpoints()+upgradesForThisUnit.hitpointBonus; 
    fusionArmor=getFusionArmor()+upgradesForThisUnit.fusionArmorBonus; 
    normalArmor=getNormalArmor()+upgradesForThisUnit.normalArmorBonus; 
    //... 
} 

Nhưng tôi ' m không chắc chắn điều này hoạt động như mong đợi.

Giải pháp này có lẽ là đơn giản nhưng tôi không biết cách giải quyết vấn đề này ngay bây giờ.

Trả lời

0

Nếu theo từ điển từ điển, bạn có nghĩa là một cái gì đó xấp xỉ như Dictionary<Player, Dictionary<Type, Upgrade>> thì, có, nó sẽ hoạt động như bạn mong đợi, và là một giải pháp tốt cho vấn đề như đã nêu. Bạn cũng có thể sử dụng một loạt các từ điển nếu bạn có một số lượng nhỏ người chơi tối đa, bạn không thực sự nhận được bất kỳ thứ gì hữu ích quá mức khi băm 4 giá trị. (Nếu bạn có tối đa 4 người chơi)

1
public class Singleton<T> 
    { 
     private static string _value; 
     public string Value 
     { 
      get 
      { 
       return _value; 
      } 
      set 
      { 
       _value = value; 
      } 
     } 
    } 
    public class At<T> 
    { 
     public static Singleton<T> field = new Singleton<T>(); 
    } 

    public class Bt : At<Bt> 
    { 
    } 

    public class Ct : At<Ct> 
    { 
    } 
... 
    Bt.field.Value = "bt"; 
    Ct.field.Value = "ct"; 
0

Câu hỏi khá cũ, nhưng, để có lợi cho mọi người trong tương lai, đây là cách tôi gặp vấn đề tương tự.

class A 
{ 
    /* Other stuff in my class*/ 
    protected MyClass nonStaticMember; 
} 

class B : A 
{ 
    private static B _instance; 
    public static B instance 
    { 
     get 
     { 
      return _instance; 
     } 
    } 
    static B() 
    { 
     _instance = new B(); 
    } 
    /* Other stuff in my class*/ 
    // Will have B.instance.nonStaticMember 
} 

class C : A 
{ 
    private static C _instance; 
    public static C instance 
    { 
     get 
     { 
      return _instance; 
     } 
    } 
    static C() 
    { 
     _instance = new C(); 
    } 
    /* Other stuff in my class*/ 
    // Will have C.instance.nonStaticMember 
} 

Giả sử bạn có thể muốn truy cập C.instance.doSomethingTo_nonStaticMember() từ một lớp khác.

Các vấn đề liên quan