我应该使用什么变量类型来存储非密封类或接口的类型? [英] What variable type should I use to store types of a non sealed class or interface?

查看:172
本文介绍了我应该使用什么变量类型来存储非密封类或接口的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

README




  • 我有数以千计的生物,每个生物都来自
    生物

  • Creature_MoonDancer 是一个生物的例子。


  • 我想找到一种方法来扩展从
    继承的任何密封类型

  • 每个生物最多只能链接到一个生物






p>这里是 CreatureData ,包含需要输入到 Creature 构造函数中的信息的类:

   -  CreatureData.cs 

使用UnityEngine.UI;

[System.Serializable]
class CreatureData {
//由Unity引擎的检查器设置。
[SerializeField] private Sprite生物头
[SerializeField] private int creatureStartingDamage;
Sprite CreatureHead {
get {
return creatureHead;
}
}
Sprite CreatureStartingDamage {
get {
return creatureStartingDamage;
}
}
}






这里我有一个抽象类生物,它包含一个生物是什么。

  --Creature.cs 

public class Creature {
protected Sprite creatureHead;
protected int creatureDamage;
public Creature(CreatureData creatureData){
creatureHead = creatureData.CreatureHead;
creatureDamage = creatureData.CreatureStartingDamage
}
public abstract void getAttackedBy(Creature other);
}






源自生物的生物类型,例如:

   -  Creature_MoonDancer.cs 

public sealed Class Creature_MoonDancer:Creature {
private int moonOrbsInPossession;
public Creature_MoonDancer(CreatureData creatureData,int startingMoonOrbs):base(creatureData){
moonOrbsInPossession = startingMoonOrbs;
}
public override void getAttackedBy(Creature other){
if(moonOrbsInPossession> other.creatureDamage){
Debug.Log(MoondancerWins);
}
else if(Random.value< 0.5)
Debug.Log(MoondancerDies);
else
Debug.Log(MoondancerWins);
}
}






CreatureDatabase将包含存储 CreatureData 字典,并将源自生成的任何密封类最多链接到1个相应的 CreatureData as such:

   -  CreatureDatabase.cs 

public class CreatureDatabase:MonoBehaviour {
private static class CreatureDataCustomSerializableDictionary
:CustomSerializableDictionary< typeof(Creature),CreatureData> {}
// TODO:为CustomSerializableDictionary创建自定义属性drawer
[SerializeField] private CreatureDataCustomSerializableDictionary creatureDatabaseDictionary;

public CreatureDatabaseDictionary {
get {
return creatureDatabaseDictionary;
}
}
}






以下是游戏经理尝试从creatureDatabaseDictionary 字典中获取值:

   -  GameManager.cs 

public class GameManager:MonoBehaviour {
[SerializeField] private CreatureDatabase creatureDatabase;
private生物SummonMoonDancer(){
CreatureData生物数据;
bool success = creatureDatabase.CreatureDatabaseDictionary.TryGetValue(typeof(Creature_MoonDancer),out creatureData);
if(success == false){
Debug.LogError(生物数据库没有typeof(MoonDancer)键时尝试召唤MoonDancer);
}
else {
return new Creature_MoonDancer(creatureData),Player.GetInt(NumberOfMoonorbsInPlayersPouch);
}

}
}






以下是一些项目要求:




  • 游戏平衡团队需要平衡游戏
    修改代码。

  • 重要:游戏平衡团队不能使用
    键来插入任何keyvaluepairs, 。


解决方案

这是您编写的代码,以便编译示例赋值:

  public interface mybaby {} 
public class mytom:mybaby {}
public class mydick:mybaby {}
public class myharry:mybaby {}

public class Test
{
mybaby mychild = new myharry();
}

这是有效的C#代码。


README

  • I have thousands of creatures, and each creature derives from Creature.
  • An example of a creature is Creature_MoonDancer.
  • Creature is a class that can only be instantiated if a CreatureData object is passed into its constructor.
  • I want to find a way to scope any sealed type that inherits from Creature to one CreatureData in a Dictionary.
  • Each creature may only be linked up to a maximum of 1 instance of CreatureData in creatureDatabaseDictionary.

Here is CreatureData, a class with information that needs to be fed into Creature's constructor to instantiate it:

--CreatureData.cs

using UnityEngine.UI;

[System.Serializable]
class CreatureData {
    //To be set by Unity engine's inspector.
    [SerializeField] private Sprite creatureHead;
    [SerializeField] private int creatureStartingDamage;
    Sprite CreatureHead {
        get{
            return creatureHead;
        }
    }
    Sprite CreatureStartingDamage {
        get{
            return creatureStartingDamage;
        }
    }
}


Here I have an abstract class Creature which encompasses what a creature is.

--Creature.cs

public class Creature {
    protected Sprite creatureHead;
    protected int creatureDamage;
    public Creature(CreatureData creatureData){
        creatureHead = creatureData.CreatureHead;
        creatureDamage = creatureData.CreatureStartingDamage
    }
    public abstract void getAttackedBy(Creature other);
}


There are thousands of sealed creature types that derive from Creature and here is one example:

--Creature_MoonDancer.cs

public sealed class Creature_MoonDancer : Creature{
    private int moonOrbsInPossession;
    public Creature_MoonDancer (CreatureData creatureData, int startingMoonOrbs) : base(creatureData){
        moonOrbsInPossession = startingMoonOrbs;
    }
    public override void getAttackedBy(Creature other){
        if(moonOrbsInPossession > other.creatureDamage){
            Debug.Log("MoondancerWins");
        }
        else if(Random.value < 0.5)
            Debug.Log("MoondancerDies");
        else
            Debug.Log("MoondancerWins");
    }
}


CreatureDatabase will contain a Dictionary that stores CreatureData and links any sealed class that derives from Creature to up to 1 corresponding CreatureData as such:

--CreatureDatabase.cs

public class CreatureDatabase : MonoBehaviour {
    private static class CreatureDataCustomSerializableDictionary
        : CustomSerializableDictionary<typeof(Creature),CreatureData> {}
    //TODO: Create custom property drawer for CustomSerializableDictionary
    [SerializeField] private CreatureDataCustomSerializableDictionary creatureDatabaseDictionary;

    public CreatureDatabaseDictionary {
        get{
            return creatureDatabaseDictionary;
        }
    }
}


Here is the game manager trying to get values out from the creatureDatabaseDictionary Dictionary:

--GameManager.cs

public class GameManager : MonoBehaviour{
    [SerializeField] private CreatureDatabase creatureDatabase;
    private Creature SummonMoonDancer(){
        CreatureData creatureData;
        bool success = creatureDatabase.CreatureDatabaseDictionary.TryGetValue(typeof(Creature_MoonDancer), out creatureData);
        if(success == false){
            Debug.LogError("Tried summoning MoonDancer when creature database did not have a typeof(MoonDancer) key");
        }
        else{
            return new Creature_MoonDancer(creatureData), Player.GetInt("NumberOfMoonorbsInPlayersPouch");
        }

    }
}


Here are some of the project requirements:

  • The game balancing team are required to balance the game without modifying code.
  • Important: The game balancing team must not be able to insert any keyvaluepairs with keys that are not creature types.

解决方案

Here's your code written so that it compiles with an example assignment:

public interface mybaby {}
public class mytom : mybaby {}
public class mydick : mybaby {}
public class myharry : mybaby {}

public class Test
{
    mybaby mychild = new myharry();
}

This is working, valid, C# code.

这篇关于我应该使用什么变量类型来存储非密封类或接口的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆