C#8 接口与其中定义的 properties/methods - 显然不工作
C#8 interfaces with properties/methods defined in them - apparently not working
这是我使用的界面。
public interface IPresentism {
public abstract bool isPresent { get; }
public virtual bool isAbsent => !isPresent;
}
isPresent 是抽象的,因此接口用户必须实现它。
Interface 还带有一个方便的否定,isAbsent,它 是 实现的,并且是虚拟的,所以它应该被添加到它混合的所有子类中(实际上它从未被覆盖但是不管怎样,从 isAbsent 中删除 'overide' 不会改变任何东西)。然后尝试使用 IPresentism..
public class tester : IPresentism {
public bool isPresent => true;
public bool checkAbsentVisible =>
this.isAbsent; // fails
}
错误是
Error CS1061 'tester' does not contain a definition for 'isAbsent' and no accessible extension method 'isAbsent' accepting a first argument of type 'tester' could be found.
C#8 应该支持这些“您现在可以向接口添加成员并为这些成员提供实现”https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#default-interface-methods。也许真的是方法而不是属性,试试看:
public interface IPresentism {
public abstract bool isPresent(); // { get; }
public virtual bool isAbsent() => !isPresent();
}
public class tester : IPresentism {
public bool isPresent() => true;
public bool checkAbsentVisible =>
this.isAbsent(); // nope again
}
同样的错误。我误会了什么?
规则是接口中声明的默认方法只有在将对象转换为接口类型时才可用。
这背后的原因既有技术上的,也有理论上的,并进行了解释here。这是引述,我在其中加了一些强调:
That cast from SampleCustomer
to ICustomer
is necessary. The SampleCustomer
class doesn't need to provide an implementation for ComputeLoyaltyDiscount; that's provided by the ICustomer
interface. However, the SampleCustomer class doesn't inherit members from its interfaces. That rule hasn't changed. In order to call any method declared and implemented in the interface, the variable must be the type of the interface, ICustomer
in this example.
效果是这样编译不通过:
public bool checkAbsentVisible => this.isAbsent();
虽然这没问题:
public bool checkAbsentVisible => ((IPresentism)this).isAbsent();
这也是:
public bool checkAbsentVisible => (this as IPresentism).isAbsent();
这是我使用的界面。
public interface IPresentism {
public abstract bool isPresent { get; }
public virtual bool isAbsent => !isPresent;
}
isPresent 是抽象的,因此接口用户必须实现它。
Interface 还带有一个方便的否定,isAbsent,它 是 实现的,并且是虚拟的,所以它应该被添加到它混合的所有子类中(实际上它从未被覆盖但是不管怎样,从 isAbsent 中删除 'overide' 不会改变任何东西)。然后尝试使用 IPresentism..
public class tester : IPresentism {
public bool isPresent => true;
public bool checkAbsentVisible =>
this.isAbsent; // fails
}
错误是
Error CS1061 'tester' does not contain a definition for 'isAbsent' and no accessible extension method 'isAbsent' accepting a first argument of type 'tester' could be found.
C#8 应该支持这些“您现在可以向接口添加成员并为这些成员提供实现”https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#default-interface-methods。也许真的是方法而不是属性,试试看:
public interface IPresentism {
public abstract bool isPresent(); // { get; }
public virtual bool isAbsent() => !isPresent();
}
public class tester : IPresentism {
public bool isPresent() => true;
public bool checkAbsentVisible =>
this.isAbsent(); // nope again
}
同样的错误。我误会了什么?
规则是接口中声明的默认方法只有在将对象转换为接口类型时才可用。
这背后的原因既有技术上的,也有理论上的,并进行了解释here。这是引述,我在其中加了一些强调:
That cast from
SampleCustomer
toICustomer
is necessary. TheSampleCustomer
class doesn't need to provide an implementation for ComputeLoyaltyDiscount; that's provided by theICustomer
interface. However, the SampleCustomer class doesn't inherit members from its interfaces. That rule hasn't changed. In order to call any method declared and implemented in the interface, the variable must be the type of the interface,ICustomer
in this example.
效果是这样编译不通过:
public bool checkAbsentVisible => this.isAbsent();
虽然这没问题:
public bool checkAbsentVisible => ((IPresentism)this).isAbsent();
这也是:
public bool checkAbsentVisible => (this as IPresentism).isAbsent();