我具有以下设置:Entity
来自MonoBehaviour
。MonoBehaviour实现了对bool的隐式转换。现在,如果我在Entity中实现了对bool的隐式转换,它将覆盖MonoBehaviour中的那个。如果现在我想同时访问旧转换和新转换,则必须将其转换回基类
public class Entity : MonoBehaviour
{
private float CurrentHealthPoints { get; set; }
public static implicit operator bool(Entity entity)
=> (MonoBehaviour)entity && entity.CurrentHealthPoints > 0;
}
现在我的问题是,有没有一种不必强制转换为基类的方法?我尝试使用base
关键字,但无法使其正常工作。
据我所知,除非所使用的类型使用contravariance,否则显式转换是不可避免的。您也可以使用the link。