下面的格式是 Java 中的某种约定吗?我个人觉得它比第二个例子更难读。我一个人在这里吗?选择“源”->“格式”时,是否有办法在 NetBeans 中制作此类自定义格式?
public boolean handleEvent(Event e) {
if (e.target == quit) System.exit(0);
else if (e.target == parent) {
// Code here
// Bug workaround
}
catch (IOException ex) { infoarea.setText("I/O Error"); }
return true;
}
(第二个例子)
public boolean handleEvent(Event e)
{
if (e.target == quit) System.exit(0);
else if (e.target == parent)
{
// Code here
// Bug workaround
}
catch (IOException ex) { infoarea.setText("I/O Error"); }
return true;
}
最佳答案
维基百科有一个 whole article about indentation style . Oracle 已设置 Java coding conventions和一个 style guide PDF .这确实是一个品味问题,但是存在特定于 Java 的准则,以便所有其他 Java 开发人员能够快速、良好地理解和维护所有 Java 代码。我讨厌在 Java 代码中看到 C# 风格,但反过来,我也讨厌在 C# 代码中看到 Java 风格。 Java 代码使用 Java 风格,C# 代码使用 C# 风格。它让每个人都清楚一切。
您的示例无法编译,但假设确实没有 try
,下面是如何以干净的 Java 方式进行编译:
public boolean handleEvent(Event e) {
if (e.target == quit) {
System.exit(0);
} else if (e.target == parent) {
// Code here
// Bug workaround
} catch (IOException ex) {
infoarea.setText("I/O Error");
}
return true;
}
您呈现的第一种样式看起来很像 banner style第二个很像GNU style (有点类似于 Allman / C# style 。
关于Java格式化风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3503565/