C#CS0266使用;这";指向类的当前实例
我正在创建一个程序,一个人可以装备一个三明治。我一直收到错误CS0266,我不知道为什么?我用它来指代这个人的当前实例C#CS0266使用;这";指向类的当前实例,c#,debugging,inheritance,C#,Debugging,Inheritance,我正在创建一个程序,一个人可以装备一个三明治。我一直收到错误CS0266,我不知道为什么?我用它来指代这个人的当前实例 class Sandwich : IEquip { public void Equip(Person person) { person.EquippedSandwich = this; //I get the error on "this" here. } ... } interface IEquip
class Sandwich : IEquip
{
public void Equip(Person person)
{
person.EquippedSandwich = this; //I get the error on "this" here.
}
...
}
interface IEquip
{
void Equip(Person person);
}
通常,当代码尝试在两种类型之间进行转换时会出现此错误,这两种类型不能进行隐式转换,但可以进行显式转换 您需要使用
equipedSandwich
所属的类型执行显式强制转换
person.EquippedSandwich = (IEquip)this;
什么类型的
装备三明治
?您可以显示Person
类的定义吗?您可以显示Person
类吗?“我用它来表示Person的当前实例。”???句子听起来很奇怪-样本中的这个
属于三明治
类型,不太可能是个人…私人三明治
person.EquippedSandwich = (IEquip)this;