有没有在Java中为那些为满足接口空方法成语? [英] Is there an idiom in Java for empty methods which exist to satisfy an interface?

查看:257
本文介绍了有没有在Java中为那些为满足接口空方法成语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个类实现接口,如的MouseListener 。在的MouseListener 接口包括5个方法,但我只希望覆盖其中之一(的mouseClicked())。有没有一个标准,格式化等方法惯用的方法是什么?

Let's say I have a class Foo implementing an interface such as MouseListener. The MouseListener interface consists of five methods but I only wish to override one of them (mouseClicked()). Is there a standard, idiomatic way of formatting the other methods?

我的倾向是编写如下:

@Override
public void mouseClicked(MouseEvent e) {
    // (...) <-- actual code here
}

@Override
public void mouseEntered(MouseEvent e) {
    // Do nothing.  Exists to satisfy MouseListener interface.
}

@Override
public void mouseExited(MouseEvent e) {
    // Do nothing.  Exists to satisfy MouseListener interface.
}

@Override
public void mousePressed(MouseEvent e) {
    // Do nothing.  Exists to satisfy MouseListener interface.
}

@Override
public void mouseReleased(MouseEvent e) {
    // Do nothing.  Exists to satisfy MouseListener interface.
}

我摆明了方法是有意空白,而不是不小心留下这样的粉丝,但我不是疯了放弃的基本上没什么所有的垂直空间。我也看到的格式如下:

I'm a fan of making it explicit that methods are intentionally blank rather than accidentally left so, but I'm not crazy about all the vertical space given up for basically nothing. I've also seen the following format:

public void mouseClicked(MouseEvent e) {
    // (...) <-- actual code here
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}

我一般用这种确定,我理解作者的意图,但它变得非常丑陋的(<一当href=\"http://stackoverflow.com/questions/212614/should-a-method-that-implements-an-interface-method-be-annotated-with-override\">recommended) @覆盖注释添加。

我不是一个特别有经验的Java codeR所以我想我会问,如果有一个约定。思考?

I'm not a particularly experienced Java coder so I figured I'd ask if there was a convention. Thoughts?

推荐答案

我做的你做,如果theres什么也没有留出一条线以同样的方式。也许把评论的执行单行大块的顶部。

I do it the same way you do, if theres nothing there leave at one line. Maybe put a comment on top of a large block of 'implementation one-liners'.

这篇关于有没有在Java中为那些为满足接口空方法成语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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