2013-08-11 6 views
1

자바 코드를 C#으로 포팅하고이 코드를 발견했습니다. 클래스 배열을 선언하는 것처럼이 방법을 이식 할 수있는 방법을 모르겠습니다. C#의 내 지식은 C#에서이 동작이 어떻게 달성되는지 파악하기에는 너무 제한적입니다. C#에서이 스텁 선언에 해당하는 것이 있습니까? 어떻게 생겼습니까? 10 개의 객체를 선언했기 때문에 최소로 줄였습니다. 여기에 코드를 보이는 방법은 다음과 같습니다C와 같은 Java 코드는 어떻게 나타 납니까?

 public interface IcodecWeightFunctionStub 
     { 
       void hcodec_weight_func(int[] block, int block_offset, int stride, int log2_denom, int weight, int offset); 
     } 

     public interface IHcodecBiWeightFunctionStub 
     { 
       void hcodec_biweight_func(int[] dst, int dst_offset, int[] src, int src_offset, int stride, int log2_denom, int weightd, int weights, int offset); 
     } 


     public IHcodecWeightFunctionStub[] weight_hcodec_pixels_tab = new IHcodecWeightFunctionStub[]    { 
    new IHcodecWeightFunctionStub() { 
      public void hcodec_weight_func(int []block, int block_offset, int stride, int log2_denom, int weight, int offset) { 
        weight_hcodec_pixels_c(16, 16, block, block_offset, stride, log2_denom, weight, offset); 
      } 
    }, 
    new IHcodecWeightFunctionStub() { 
      public void hcodec_weight_func(int []block, int block_offset, int stride, int log2_denom, int weight, int offset) { 
        weight_hcodec_pixels_c(16, 8, block, block_offset, stride, log2_denom, weight, offset); 
      } 
    } 
}; 

문제는 C#에서 인터페이스의 인스턴스에 대한 것이 아니라 C#으로 새로운 클래스를 반환에 대한 자세한.

+1

복사, 비주얼 스튜디오에 붙여 넣습니다 당신을주는 무슨 오류 및 위글 라인을 참조하십시오

public interface Foo { void Bar(int i); } class test { //array of anonymous inner classes: public Foo[] x = new Foo[] { new Foo() { public void Bar(int i) { //code 1 } }, new Foo() { public void Bar(int i) { //code 2 } } }; } 

다음 C# 코드는 동일합니다. –

+0

그것이 내가있는 곳입니다. 오류는'Expected class, delegate, enum, interface, or struct'입니다. 나는 그것을'행동'으로 바꾸려고 노력할 것이다. 아마도. –

답변

3

익명 대리인 구현과 C#의 람다는 익명 인터페이스 구현에 가장 근접하지만 인터페이스에는 단일 메서드가 있어야합니다. 대표를 호출하면, 다른 것을

public delegate void HcodecWeightDelegate(int[] block, int block_offset, int stride, int log2_denom, int weight, int offset); 
public delegate void HcodecBiweightDelegate(int[] dst, int dst_offset, int[] src, int src_offset, int stride, int log2_denom, int weightd, int weights, int offset); 

public HcodecBiweightDelegate[] WeightHcodecPixelsTab = new HcodecBiweightDelegate[] { 
    (block, block_offset, stride, log2_denom, weight, offset) => { 
     weight_hcodec_pixels_c(16, 16, block, block_offset, stride, log2_denom, weight, offset); 
    } 
, (block, block_offset, stride, log2_denom, weight, offset) => { 
     weight_hcodec_pixels_c(16, 8, block, block_offset, stride, log2_denom, weight, offset); 
    } 
}; 

참고도 :

weight_hcodec_pixels_tab[i].hcodec_weight_func(block, block_offset, stride, log2_denom, weight, offset); 
:이처럼 그들을 부를 것이다 자바에 다음과 같이 두 당신의 인터페이스는 하나의 메소드, 그래서 당신의 코드는 C 번호로 변환 할 수있다 (대표이 하나의 방법을 캡슐화하기 때문에) C#에서

, 당신은 방법 이름이없는, 그래서 같은 호출은 다음과 같을 것이다 :

HcodecBiweightDelegate[i](block, block_offset, stride, log2_denom, weight, offset); 
+0

와우! 마지막 질문 :이 인스턴스가 인터페이스 인 경우 이처럼 빈 클래스를 만들 수 있습니까? 'public class HcodecEtc : IHcodecEtc {}' –

+1

@ LéonPelletier 이것은 실제로 인터페이스가 아닙니다. 인터페이스를 가지고있는 함수 포인터에 더 가깝습니다. 빈 대리자를 만들 수는 없지만 비어있는 본문 (즉, 아무 것도 수행하지 않는 대표자)이있는 대리인이있을 수 있습니다. 당신이 필요로하는 것은 같은 문법을 따르는 것이지만 다음과 같이 중괄호를 공백으로 남겨 두는 것입니다 :'(block, block_offset, stride, log2_denom, weight, offset) => {}' – dasblinkenlight

+1

@ LéonPelletier 코드의 첫 부분은 있는 그대로의 상태로 번역됩니다. 문제는 각 익명 인터페이스 구현에 대해 명명 된 클래스를 선언하지 않고서는 나머지 절반을 작성할 수 없다는 것입니다. – dasblinkenlight

1

귀하의 예는 거의 일치합니다 - interfa ce 메서드 이름은 일치하지 않지만 잘하면 배열 예제와 C#이 도움이 될 것입니다. 델리게이트와 람다를 사용할 수는 있지만 그렇게 할 필요는 없습니다. 다음 Java 코드에 대한 :

public interface Foo 
{ 
    void Bar(int i); 
} 

class Test 
{ 
    public Foo[] x = new Foo[] { new FooAnonymousInnerClassHelper1(), new FooAnonymousInnerClassHelper2() }; 

    private class FooAnonymousInnerClassHelper1 : Foo 
    { 
     public virtual void Bar(int i) 
     { 
      //code 1 
     } 
    } 

    private class FooAnonymousInnerClassHelper2 : Foo 
    { 
     public virtual void Bar(int i) 
     { 
      //code 2 
     } 
    } 
} 
관련 문제