2014-04-25 6 views
1

인터페이스 (ItestClass)를 통해 기본 클래스 (testClass) 속성에 액세스 할 수없는 이유는 무엇입니까? 실제 컨트롤 (winforms/wpf) 속성이 세 번째 클래스 (newClass)에 표시되지 않도록 인터페이스를 만들었습니다. 그게 가능하지 않다면 더 좋은 방법이 있을까요?클래스 인터페이스를 통해 클래스의 속성에 액세스

public class testClass : Control, ItestClass 
{ 
    public int Property1 { set; get; } 
    public int Property2 { set; get; } 
    public testClass() { } 
} 
public interface ItestClass 
{ 
    int Property1 { get; set; } 
    int Property2 { get; set; } 
} 

public class newClass : ItestClass 
{ 
    public newClass() 
    { 
     // Why are the following statements are not possible? 
     Property1 = 1; 
     // OR 
     this.Property1 = 1; 
    } 
} 

답변

5

실제로 속성을 구현하지 않는 인터페이스 - 여전히 구현 클래스를 정의해야합니다

C#을 여러 지원하지 않습니다

public class newClass : ItestClass 
{ 
    int Property1 { get; set; } 
    int Property2 { get; set; } 

    // ... 
} 

편집 상속 때문에 testClassControl과 다른 구체적인 클래스를 상속받을 수 없습니다. 당신은 언제나 대신 컴포지션을 사용할 수 있습니다. 예 :

public interface ItestClassProps 
{ 
    public ItestClass TestClassProps { get; set; } 
} 

public class testClass : Control, ItestClassProps 
{ 
    public ItestClass TestClassProps { set; get; } 

    public testClass() { } 
} 
+0

@mason은 'newClass'가 아닙니다. –

+0

문제는 단순히 모든 test 속성을 보여주기 때문에 testClass를 직접 상속받지 않으려 고합니다. 대신 피할 수있는 인터페이스를 상속받습니다. – Johnny

+0

기본적으로 세 번째 클래스 (newClass)에서 다시 정의해야합니다. – Johnny

관련 문제