本文介绍了C#V5接口的Java实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我怎么想去了解我的问题。这是关于Android可以实例化接口。我想在C#中的事。现在,我敢肯定,对于Java和C#的规则是,你不能创建抽象和接口的实例作为被说。

I am not sure how am I suppose to go about my question. It is about Android can Instantiate Interface. I am trying to do in C#. Now I am pretty sure that the rules for both Java and C# is you can't create an Instance of abstract and Interface as being said.

不过,我真的想了解Android如何做到这一点的做法。

But I would really like to know how Android does this practice.

在Android中,你可以做到这一点。

In Android you can do this.

public interface Checkme{
    void Test();
    void Test2();
}


public void myFunc(Checkme my){
  //do something
}

// Now this is the actual usage.

public void Start(){

   myFunc(new Checkme(){
       @Override
       public void Test()
       {


       }
       @Override
       public void Test2()
       {


       }
    });

}



事实上,一旦你按Enter键新Checkme()您将自动获得该接口的覆盖方法。如自动C#实现接口的方法。

Actually once you press Enter on new Checkme() You will automatically get the Override methods of the Interface. Like auto Implement method of an Interface in C#.

我希望我的问题是有意义的。

I hope my question make sense.

推荐答案

C#不支持匿名自动实现的接口,因为它的代表的:

C# doesn't support anonymously auto-implemented interfaces because it has delegates:

public void Foo(Func<string> func, Action action) {}

// call it somewhere:
instance.Foo(() => "hello world", () => Console.WriteLine("hello world"));

通过代表可以填补这一空白,它甚至比实现具有匿名类接口,功能更强大。

With delegates you can fill the gap and it can be even more powerful than implementing interfaces with anonymous classes.

这篇关于C#V5接口的Java实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:11