c# - 字典实例覆盖
问题描述
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?我确实阅读了这一点,并找到了 getter 的答案,但它涉及为每个对象创建多个字典,这不是我想要的。
解决方案
没有理由覆盖任何东西。您可以在构造函数中创建一个新字典:
public class Plains : Turf
{
public Plains()
{
allowed_contents = new Dictionary<Type, int>()
{
{typeof(Forest), 100},
{typeof(Tall_Grass), 25},
{typeof(Mountain), 10}
};
}
}
推荐阅读
- java - 生成用于在 Java 中访问 CR 的身份验证令牌
- shell - 如何在 Ubuntu shell 中为当前日期添加一天?
- json - 应用程序中未使用 JSON 文件的更改 - Flutter
- c# - 将参数传递给数据访问层时出错
- python - 如何在 Python 中关联两个音频事件(检测它们是否相似)
- c# - 如何获取 SQL Server 数据库文件连接字符串以在 Visual Studio 2013 中部署项目?
- javascript - 异步等待承诺待处理
- google-apps-script - 如何修改此功能以对每个选项卡使用多个选项卡和列?
- c# - 评估嵌套在 foreach 中的值和国际象棋中的 if 方法
- python - 一起使用过采样和 cross_validation 函数的方法是什么?