在使用C#中,经常会遇到多种接口继承,比如下像下面的代码:
- public class Health : IHealth, ISync, ITick, INotifyCreated, INotifyOwnerChanged
- {
- public readonly HealthInfo Info;
- INotifyDamageStateChanged[] notifyDamageStateChanged;
- INotifyDamage[] notifyDamage;
- INotifyDamage[] notifyDamagePlayer;
- IDamageModifier[] damageModifiers;
- IDamageModifier[] damageModifiersPlayer;
- INotifyKilled[] notifyKilled;
- INotifyKilled[] notifyKilledPlayer;
在这里看到这个类,它总共继承了5个接口。
由于接口继承比较多,那么在写代码的时候要注意什么呢?
这种情况下,由于接口众多,那么出现接口函数命名重复的机率就会增多,导致编译不通过。这时就需要改写接口时,把接口名字也要写上去。
如下所示:
- void INotifyCreated.Created(Actor self)
- {
- notifyDamageStateChanged = self.TraitsImplementing
().ToArray(); - notifyDamage = self.TraitsImplementing
().ToArray(); - notifyDamagePlayer = self.