2016-07-06 32 views
1

同时使Java中的小游戏,我偶然发现了KeyListener的类,实例化(的keyTyped,的keyPressed和的keyReleased)时,询问了三种方法,如下:如何制作方法的参数的构造函数

JFrame.addKeyListener(new KeyListener(){ 
    public void keyTyped(KeyEvent evnt) { 
    //blah 
    } 
    public void keyPressed(KeyEvent evnt) { 
    //blah 
    } 
    public void keyReleased(KeyEvent evnt) { 
    //blah 
    } 
}); 

我怎样才能得到一个我自己的接受方法作为上述参数的类?

+1

看看这个,http://stackoverflow.com/questions/4685563/how-to-pass -a-功能作为一种参数中的Java –

回答

1

new KeyListener() { ... }实际上创建了一个实现KeyListener的匿名内部类。因此,它可以访问创建它的类的任何可见字段以及调用构造方法的方法内的任何本地最终变量。

例子:

class Outer { 
    int x; 

    void initUI(final int z) { 
    final int y = 0;   
    int nope = 1; //you can't access this since it is neither final nor a field like x 

    JFrame.addKeyListener(new KeyListener() { 
     public void keyTyped(KeyEvent evnt) { 
     System.out.println(x + y + z); //you can access all of them 
     } 
    }); 
    } 
} 

如果你想为你的按键侦听器,您需要明确地定义一个类,作为anonymous classes can't have custom constructors提供一个构造函数。这意味着你必须做这样的事情(注意:伪代码):

class Outer { 
    void initUI() { 
    JFrame.addKeyListener(new InnerKeyListener(myParam)); 
    } 

    class InnerKeyListener implements KeyListener { 
    InnerKeyListener(Param p) { 
    } 

    ... 
    } 
} 

当然你也可以把内部类成separe文件以及或使其静态内部类。

0

它不接受方法作为参数。你声明了一个匿名类并将其作为参数传递给addKeyListener方法。

1

从技术上讲,这些方法在这里不是参数。唯一的参数是KeyListener的一个匿名实例。

KeyListener是interface,它要求实现这3种方法。

如果要定义自己的接口,它类似于一个类:

public interface SomeInterface { 
    public void foo(); 
    public int bar(int x); 
} 

然后你就可以(在你的例子一样)匿名方式使用它,或者在一个类实现它:

public class MyClass implements SomeInterface { 
    // ... 
} 
0

我知道这是怪异回答我的问题,但...

我想通了。您必须使您的类变为抽象类型,并且您可以声明抽象方法来使方法“参数”具有类似于KeyListener声明的行为。抽象方法声明如下:

abstract ReturnType name(Parameter p); 

并且在调用时的行为与方法完全相同。举一个完整的例子:

//The abstract keyword enables abstract methods to work with this class. 
    abstract class Foo { 
     //Just a random variable and a constructor to initialize it 
     int blah; 
     public Foo(int blah) { 
      this.blah = blah; 
     } 
     //An abstract method 
     abstract void doStuff(); 
     //Example with a parameter 
     abstract void whatDoIDoWith(int thisNumber); 
     //A normal method that will call these methods. 
     public void doThings() { 
      //Call our abstract method. 
      doStuff(); 
      //Call whatDoIDoWith() and give it the parameter 5, because, well... 5! 
      whatDoIDoWith(5); 
     } 
    } 

当你试图实例化一个像普通类的抽象类时,Java会吓坏了。

Foo f = new Foo(4); 

你将需要做的就是这样的事情:你需要包括所有的抽象方法在这里

Foo f = new Foo(4) { 
     @Override 
     void doStuff() { 
      System.out.println("Hi guys! I am an abstract method!"); 
     } 
     @Override 
     void whatDoIDoWith(int thisNumber) { 
      blah += thisNumber; 
      System.out.println(blah); 
     } 
    }; //Always remember this semicolon! 

注意,不只是其中的一部分。现在,让我们试试这个:

public class Main { 
     public static void main(String[] args) { 
      //Instance foo. 
      Foo f = new Foo(4) { 
       @Override 
       void doStuff() { 
        System.out.println("Hi guys! I am an abstract method!"); 
       } 
       @Override 
       void whatDoIDoWith(int thisNumber) { 
        blah += thisNumber; 
        System.out.println(blah); 
       } 
      }; 
      //Call our method where we called our two abstract methods. 
      foo.doThings(); 
     } 
    } 

这将打印以下控制台:

Hi guys! I am an abstract method! 
    9 
相关问题