2010-03-12 14 views

回答

2

这听起来像你在找什么是anonymous classes

他们最常用的是这样的:

interface Something { void execute(); } 

// In some code... 
setSomething(new Something() { 
    public void execute() { 
     // Your code here 
    } 
}); 
0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication2 
{  
    interface ISampleInterface 
    { 
     void SampleMethod(); 
    } 

    class ImplementationClass : ISampleInterface 
    { 
     // Explicit interface member implementation: 
     public void SampleMethod() 
     { 
      // Method implementation. 
      Console.Write("Interface implements"); 
     } 

     static void Main(string[] args) 
     { 
      // Declare an interface instance. 
      ISampleInterface obj = new ImplementationClass(); 

      // Call the member. 
      obj.SampleMethod(); 
      ImplementationClass a = new ImplementationClass(); 
      a.SampleMethod(); 
      ISampleInterface b = (ISampleInterface)a; 
     } 
    } 
} 
+0

这看起来像C#代码。这个问题是关于Java的。 – 2017-07-08 12:18:45

相关问题