如何合理地将派生类两两配对?附相关C#类定义
合理的Creature派生类两两配对方案
基于你给出的C#类结构,这里有一套贴合设计逻辑的NormalCreature与MutantCreature派生类配对方案,完全遵循你已经定义的Human→Superhuman的双向指向规则:
先回顾基础类结构
public abstract class Creature { public abstract int MaxLevel { get; } } public abstract class NormalCreature : Creature { public abstract Type Mutation { get; } public override int MaxLevel { get { return 50; } } } public abstract class MutantCreature : Creature { public abstract Type Base { get; } public override int MaxLevel { get { return 70; } } } public sealed class Human : NormalCreature { public override Type Mutation { get { return typeof(Superhuman); } } /* 其他逻辑 */ }
具体配对方案
每一组配对都保证Normal类指向对应的Mutant类,Mutant类反向指向Normal类,同时继承各自基类的等级上限:
1. 人类 ↔ 超级人类
这是你已经给出的配对,我们补充对应的Mutant实现:
public sealed class Superhuman : MutantCreature { public override Type Base { get { return typeof(Human); } } // 超级人类专属能力,比如超强体力、快速自愈等 }
2. 兽人 ↔ 变异兽人
// 普通兽人 public sealed class Orc : NormalCreature { public override Type Mutation { get { return typeof(MutantOrc); } } // 普通兽人逻辑:近战攻击、部落群居特性等 } // 变异兽人 public sealed class MutantOrc : MutantCreature { public override Type Base { get { return typeof(Orc); } } // 变异兽人专属逻辑:更高的防御、狂暴状态等 }
3. 精灵 ↔ 堕落精灵
// 普通精灵 public sealed class Elf : NormalCreature { public override Type Mutation { get { return typeof(FallenElf); } } // 普通精灵逻辑:远程弓箭、自然魔法亲和等 } // 堕落精灵 public sealed class FallenElf : MutantCreature { public override Type Base { get { return typeof(Elf); } } // 堕落精灵专属逻辑:黑暗魔法、夜视能力等 }
4. 矮人 ↔ 岩石矮人
// 普通矮人 public sealed class Dwarf : NormalCreature { public override Type Mutation { get { return typeof(RockDwarf); } } // 普通矮人逻辑:锻造技艺、抗毒特性等 } // 岩石矮人 public sealed class RockDwarf : MutantCreature { public override Type Base { get { return typeof(Dwarf); } } // 岩石矮人专属逻辑:岩石皮肤、地底生存能力等 }
配对核心原则
- 双向一致性:每对的两个类互相指向对方的类型,确保类型体系的严谨性,方便后续做变异/回溯的逻辑处理。
- 等级继承:所有Normal派生类自动拥有50级上限,Mutant派生类自动拥有70级上限,无需重复定义。
- 密封派生类:和
Human保持一致用sealed修饰,避免无意义的深层继承,让类型结构更清晰可控。
内容的提问来源于stack exchange,提问作者Maxime Recuerda
相关产品推荐
相关产品推荐

