匿名接口声明的正确样式约定?
Proper style conventions for anonymous interface declarations?
我正在使用 ActionListener
界面来添加 JButton
对象的交互性。这就是 Eclipse 使用 Ctrl+Shift+F
为我格式化下面代码的方式,但是当我想创建一个匿名接口时,对于这种情况,正确的样式约定是什么?
updateButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
还有这个:
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
};
假设你使用的是Java8,有两种选择:
1) 使用 lambda:
final ActionListener actionListener = e -> {
//do stuff
};
和
new JButton().addActionListener(e -> {
//do stuff
});
2) 使用方法参考:
public void toStuff(final ActionEvent e) {
//do stuff
}
然后:
final ActionListener actionListener = this::doStuff;
或
new JButton().addActionListener(this::doStuff);
更一般地说,其余的格式是基于意见的。例如,我更喜欢 Egyptian brackets。但是不是的观点是你应该对所有代码使用一致格式——让你的IDE为你做。
我正在使用 ActionListener
界面来添加 JButton
对象的交互性。这就是 Eclipse 使用 Ctrl+Shift+F
为我格式化下面代码的方式,但是当我想创建一个匿名接口时,对于这种情况,正确的样式约定是什么?
updateButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
还有这个:
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
};
假设你使用的是Java8,有两种选择:
1) 使用 lambda:
final ActionListener actionListener = e -> {
//do stuff
};
和
new JButton().addActionListener(e -> {
//do stuff
});
2) 使用方法参考:
public void toStuff(final ActionEvent e) {
//do stuff
}
然后:
final ActionListener actionListener = this::doStuff;
或
new JButton().addActionListener(this::doStuff);
更一般地说,其余的格式是基于意见的。例如,我更喜欢 Egyptian brackets。但是不是的观点是你应该对所有代码使用一致格式——让你的IDE为你做。