字典实例覆盖

时间:2018-08-17 14:50:12

标签: c# dictionary

public class Turf : Atom
{
    public Dictionary<Type, int> allowed_contents = new Dictionary<Type, int>()
    {
    };

public class Plains : Turf
{
    public new Dictionary<Type, int> allowed_contents = new Dictionary<Type, int>()
    {
        {typeof(Forest), 100},
        {typeof(Tall_Grass),  25},
        {typeof(Mountain), 10}
    };

我将如何实际覆盖allowed_contents?我确实阅读了此书,并找到了使用吸气剂的答案,但这涉及到每个对象创建多个字典,这不是我想要的。

1 个答案:

答案 0 :(得分:3)

没有理由覆盖任何内容。您可以在构造函数中创建一个新字典:

public class Plains : Turf
{
    public Plains()
    {
        allowed_contents = new Dictionary<Type, int>()
        {
            {typeof(Forest), 100},
            {typeof(Tall_Grass),  25},
            {typeof(Mountain), 10}
        };
    }
}