Java“新"的参数中的关键字 [英] Java "new" keyword in parameter

查看:88
本文介绍了Java“新"的参数中的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在研究许多OOP设计模式,并且遇到了一些以前从未见过的奇怪事情:

I've been looking through a lot of OOP Design Patterns lately, and I've run into some strange things I've never seen before:

Button button =  new Button(shell, SWT.PUSH);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
    // Handle the selection event
    System.out.println("Called!");
    }
}); 

具体来说,这是做什么的(例如,"new"关键字在这里做什么)?

Specfically, what does this do (eg. what does the "new" keyword do here)?:

    button.addSelectionListener(new SelectionAdapter() {

第二个问题:

private void notifyListeners(Object object, String property, String oldValue, String newValue) {
   for (PropertyChangeListener name : listener) {
      name.propertyChange(new PropertyChangeEvent(this, "firstName", oldValue, newValue));
   }
}

这是观察者设计模式的摘录.就我的新理解而言,name.propertyChange(...)创建一个PropertyChangeEvent对象,并通过Java的观察者模式实现,通过将该新对象的信息发送给观察者(或与此类似的事物)来自动通知观察者.这是正确的吗?

This is a snippet from an observer design pattern. To my new understanding, the name.propertyChange(...) creates an object of PropertyChangeEvent and through Java's observer pattern implementation, automatically notifies the observers by sending this new object's information to the observers (or something very similar to this). Is this correct?

推荐答案

在这里,new关键字正在创建一个匿名类.

Here, the new keyword is creating an anonymous class.

当您需要一个侦听器来执行某些操作,并且希望将代码分组在一起,并且/或者该类是一次性"时,这很有用.这意味着它在其他地方没有用.

This is useful when you need a listener to perform some action, and you'd like to keep your code grouped together, and/or the class is "one-off", meaning it has no use elsewhere.

这是指向匿名类的sun教程的链接 .所有正常的班级规则均适用.创建接口时,您需要实现抽象方法或所有方法.

Here's a link to sun's tutorial on anonymous classes. All the normal rules of classes apply. You need to implement abstract methods, or all methods when creating an interface.

作用域有些不同,因为您可以访问嵌套在您的匿名类中的类中声明的变量.但是,您不能从匿名类访问局部变量,除非这些局部变量被声明为final.例如:

Scope is a little different as you can access variables declared in the class your anonymous class is nested within. However, you can't access local variables from an anonymous class unless those local variables are declared final. For instance:

Button button =  new Button(shell, SWT.PUSH);
final String someString = "hello world!";
button.addSelectionListener(new SelectionAdapter() { 
    @Override
    public void widgetSelected(SelectionEvent e) {
        // Handle the selection event
        System.out.println(someString);
    }
});

如果someString在更全局的范围内声明,则情况并非如此.

If someString were declared in more global scope, this would not be the case.

第二个问题:

是的.您是正确的,这就是代码段中正在发生的事情.注意到每次都在创建一个新的PropertyChangeEvent吗?这样一来,出现在列表前面的侦听器就不会修改PropertyChangeEvent出现在列表后面的项目.

Yes. You are correct, that's what's happening in the snippet. Notice a new PropertyChangeEvent is being created every time? This is so listeners appearing earlier in the list don't modify the PropertyChangeEvent for items appearing later in the list.

这篇关于Java“新"的参数中的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆