2010-03-12 41 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

相關問題