2

为什么以下代码必须使用显式接口实现语法定义“CalcUsable”?(见最后一行代码)如果我使用非显式语法,(即公共十进制 CalcUsable)我会收到“不一致的可访问性”错误,具体来说:

“参数类型 ...List (IVoucher) 比方法 CalcUsable(...) 更难访问”

interface IVoucher
{
    string Serial { get; }
    decimal FaceValue { get; }
    string DiscountType { get; }
    string ApplyMsg { get; }
    string RejectMsg { get; }
    decimal CalcUsable(List<Product> products, List<IVoucher> vouchers);
}

// 'GiftVoucher' models the simple voucher which has no use restrictions.
//
public class GiftVoucher : IVoucher
{
    public string Serial { get; private set; }
    public decimal FaceValue { get; private set; }
    public string DiscountType { get; private set; }
    public string ApplyMsg { get; private set; }
    public string RejectMsg { get; private set; }

    public GiftVoucher(string serial, decimal faceValue)
    {
        Serial = serial;
        FaceValue = faceValue;
        ApplyMsg = string.Empty;
        RejectMsg = string.Empty;
        DiscountType = "Gift";
    }

    // 'CalcUsable' provides the voucher applicability logic.  
    //
    decimal IVoucher.CalcUsable(List<Product> products, List<IVoucher> vouchers)
    {
         blar, blar, blar...
4

1 回答 1

3

GiftVoucher是公共的,而接口IVoucher不是。因此,CalcUsable无法从任何IVoucher不可用的地方调用,因为该方法采用List不可访问的类型。

正如 JRLambert 建议的那样,公开接口应该可以解决这个问题。

于 2014-12-13T19:55:29.903 回答