2013-01-12 3 views
2

저는 C# 프로그래밍을 처음 접했고 아마추어 문제라는 것을 알고 있으므로 제발 웃지 마세요.인터페이스 구현 오류

난 내가 파생 된 클래스는 IInterface의 모든 멤버를 구현하지만 여전히 나는이 오류를 받아 봐 이러한 인터페이스

class derived : iInterface3 
{ 
    double[] d = new double[5]; 
    public override int MyProperty 
    { 
     get 
     { 
      return 5; 
     } 
     set 
     { 
      throw new Exception(); 
     } 
    } 
    int iProperty 
    { 
     get 
     { 
      return 5; 
     } 
    } 
    double this[int x] 
    { 
     set 
     { 
      d[x] = value; 
     } 
    } 
} 
class derived2 : derived 
{ 

} 
interface iInterface 
{ 
    int iProperty 
    { 
     get; 
    } 
    double this[int x] 
    { 
     set; 
    } 
} 
interface iInterface2 : iInterface 
{ } 
interface iInterface3 : iInterface2 
{ } 

선언했다.

는 인터페이스 부재 'final_exam_1.iInterface.this [지능] 구현하지 않는'final_exam_1.derived '. 'final_exam_1.derived.this [int]' 은 public이 아니기 때문에 인터페이스 멤버를 구현할 수 없습니다.

인터페이스 멤버 'final_exam_1.iInterface.iProperty'를 구현하지 않습니다 'final_exam_1.derived'. 'final_exam_1.derived.iProperty' public이 아니기 때문에 인터페이스 멤버를 구현할 수 없습니다.

이유는 무엇입니까?

미리 도움을 주셔서 감사합니다.

답변

3

당신은 클래스에서 파생 된 모든 회원에 publicaccess modifier를 추가해야합니다. default으로

그들은 낮은 액세스 할 수 있습니다.

또한, 당신은 인터페이스를 구현할 때 재정의 할 것이없는 한, override를 삭제해야합니다. 재정의하려면 가상 메서드를 재정의하려는 경우입니다.

class derived : iInterface3 
{ 
    double[] d = new double[5]; 

    public int MyProperty 
    { 
     get 
     { 
      return 5; 
     } 
     set 
     { 
      throw new Exception(); 
     } 
    } 

    public int iProperty 
    { 
     get 
     { 
      return 5; 
     } 
    } 

    public double this[int x] 
    { 
     set 
     { 
      d[x] = value; 
     } 
    } 
} 

코드에 다른 문제가 있지만 사안이 컴파일되지 않는 이유가 있습니다.

0

iProperty을 확인하고 인덱서 공용 또는 사용 명시 적 인터페이스 구현입니다. 명시 적 구현에 대한 선언은 다음과 같을 것입니다 : int iInterface3.iProperty. 아무것도 무시하지 있기 때문에

0

당신은하지 override 재산 int MyProperty 수 있습니다. No int MyPropertyclass/interface입니다.

0

는 요 당신은 인터페이스가 아니라 이미 가상 멤버에게 무의미 따라서 재정을 가진 기본 클래스/추상 클래스에서 구현되기 때문에 바보 문제

public override int MyProperty 
    { 
     get 
     { 
      return 5; 
     } 
     set 
     { 
      throw new Exception(); 
     } 
    } 

을 많이해야합니까.

두 번째 문제.

int iProperty 
    { 
     get 
     { 
      return 5; 
     } 
    } 

상속 재산은 개인 유형이 될 수 없습니다.

고정 코드 :

class derived : iInterface3 
{ 
    readonly double[] d = new double[5]; 
    public int MyProperty 
    { 
     get 
     { 
      return 5; 
     } 
     set 
     { 
      throw new Exception(); 
     } 
    } 

    public int iProperty 
    { 
     get 
     { 
      return 5; 
     } 
    } 

    public double this[int x] 
    { 
     set 
     { 
      d[x] = value; 
     } 
    } 
} 
class derived2 : derived 
{ 

} 
interface iInterface 
{ 
    int iProperty 
    { 
     get; 
    } 
    double this[int x] 
    { 
     set; 
    } 
} 
interface iInterface2 : iInterface 
{ } 
interface iInterface3 : iInterface2 
{ }