这样的java代码在C#中会是什么样子

本文关键字:什么样 java 代码 | 更新日期: 2023-09-27 18:27:22

我正在将一段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#返回新类。

这样的java代码在C#中会是什么样子

C#的匿名委托实现和lambdas是最接近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);

您的示例有点不一致-接口方法名称不匹配,但希望我的数组示例和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
        }
    }
}