2008-09-19 8 views
2

은 다음과 같이 수행 할 수 있는가 :인터페이스 타입 메서드 매개 변수 구현

interface IDBBase { 
    DataTable getDataTableSql(DataTable curTable,IDbCommand cmd); 
    ... 
} 

class DBBase : IDBBase { 
    public DataTable getDataTableSql(DataTable curTable, SqlCommand cmd) { 
     ... 
    } 
} 

을 내가 구현하는 인터페이스를 사용하고자하는 D/t 제공 업체 (MS-SQL, 오라클 ...); 거기에는 그것을 구현하는 해당 클래스에서 구현 될 몇 가지 서명이 있습니다. 나는 또한이 같은 시도 :

genClass<typeOj> 
{ 
    typeOj instOj; 

    public genClass(typeOj o) 
    {  instOj=o; } 


    public typeOj getType() 
    {  return instOj; } 

...

interface IDBBase 
{ 
    DataTable getDataTableSql(DataTable curTable,genClass<idcommand> cmd); 
    ... 
} 

class DBBase : IDBBase 
{ 
    public DataTable getDataTableSql(DataTable curTable, genClass<SqlCommand> cmd) 
    { 
     ... 
    } 
} 
+0

그리고 질문은 무엇입니까? –

답변

3

아니,이 수는 없습니다. 메서드는 인터페이스에서 선언 한 것과 동일한 서명을 가져야합니다.

당신이 형식 매개 변수 제약 조건을 사용할 수 있습니다 그러나

:

interface IDBClass<T> where T:IDbCommand 
{ 
    void Test(T cmd); 
} 

class DBClass:IDBClass<SqlCommand> 
{ 
    public void Test(SqlCommand cmd) 
    { 
    } 
} 
1

는 컴파일하십시오. DBBaseIDBBase을 구현하지 않으면 컴파일러에서 오류를보고합니다.

1

아니요, 불가능합니다. 나는이 컴파일 시도 :

interface Interface1 { } 
class Class1 : Interface1 {} 

interface Interface2 { void Foo(Interface1 i1);} 
class Class2 : Interface2 {void Foo(Class1 c1) {}} 

그리고 나는이 오류가 발생했습니다 :

'Class2' does not implement interface member 'Interface2.Foo(Interface1)'

3

Covariance and contravariance 널리 대표로하는 방법 그룹을 할당하는 것을 제외하고, C# 3.0로 지원되지 않습니다. 개인 인터페이스 구현을 사용하고 좀 더 구체적인 매개 변수를 사용하여 public 메서드를 호출하여 조금 에뮬레이션 할 수 있습니다.

class DBBase : IDBBase { 

    DataTable IDBBase.getDataTableSql(DataTable curTable, IDbCommand cmd) { 
     return getDataTableSql(curTable, (SqlCommand)cmd); // of course you should do some type checks 
    } 

    public DataTable getDataTableSql(DataTable curTable, SqlCommand cmd) { 
     ... 
    } 
} 
관련 문제