簡體   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