1

我正在将 java 代码移植到 C# 并遇到此代码。我不知道如何移植它,因为它似乎声明了类数组。我在 C# 方面的知识太有限,无法弄清楚如何在 C# 中实现此操作。在 C# 中是否有与此 Stub 声明等效的方法,它看起来如何?我将它缩小到最低限度,因为它声明了 10 个对象。以下是代码的外观:

        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# 中返回新类。

4

2 回答 2

3

C# 的匿名委托实现和 lambda 是最接近 Java 的匿名接口实现的东西,但接口需要有一个方法。您的两个接口都有一个方法,因此您的代码可以转换为 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);
    }
};

请注意,调用委托也是不同的:在 Java 中,您可以这样调用它们:

weight_hcodec_pixels_tab[i].hcodec_weight_func(block, block_offset, stride, log2_denom, weight, offset);

在 C# 中,您没有方法名称(因为委托封装了单个方法),因此相同的调用如下所示:

HcodecBiweightDelegate[i](block, block_offset, stride, log2_denom, weight, offset);
于 2013-08-11T01:51:47.640 回答
1

您的示例有点不一致 - 接口方法名称不匹配,但希望我的数组示例和 C# 等效项会有所帮助。您可以使用委托和 lambda,但没有必要这样做。对于以下 Java 代码:

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# 代码是等效的:

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
        }
    }
}
于 2013-08-11T17:06:42.603 回答