繁体   English   中英

使用访问修饰符的显式接口实现

[英]Explicit Interface Implementation with access modifiers

这个问题是我希望DataHelper类是内部的,因此只有同一个项目才能使用它。 但是我正在使用接口,该接口不允许访问修饰符,这将是什么方法。 谢谢

public interface IDataHelper<T>{

    int Insert(T aType)
}

//implict implementation //flat insert logic
internal class ProductDataHelper : IDataHelper<Product>{

    internal int Insert(Product aType){  //cannot use internal access modifier and dont need public

        //flat insert logic

        return 1;
    }
}

//explict implementation //flat insert logic
Public class ProductDataHelper : IDataHelper<Product>{

    int IDataHelper<Product>.Insert(Product aType){ //this becomes private

        //flat insert logic

        return 1;
    }
}

//insert with business logic
public class ProductDataHandler{

    public int Add(Product aType){

        //insert with business logic
        new Insert(Product); // this is not accessable if explict interface implementation is done

        return 1;
    }
}

接口方法的实现应该是公共的,这就是实现接口的意思。 该类仅是内部类的事实意味着该类仅由声明该类的项目所已知,但这并不妨碍该项目实例化该类并将其作为“对象”或作为公共接口传递给其他项目。 。 同时,您始终可以将接口本身声明为内部接口。

internal interface IDataHelper<T>
{
    int Insert(T aType);
}

internal class ProductDataHelper : IDataHelper<Product>
{
    public int Insert(Product aType)
    {  
        // ...
    }
}

NB相反的是你的问题的意见提出的接口中的方法明确的实现也是public即使public密钥词不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM