English 中文(简体)
java方法内的拉裂
原标题:Labled break inside java methods
  • 时间:2012-05-16 11:47:10
  •  标签:
  • java
  • loops

Why java allows to use the labled break inside a method? Is there any special purpose or use of this? I thought it can be only use within the loops and swtches.

public void testMeth(int count){
   label:
   break label;
}

但下文给出的是汇编错误。

public void testMeth(int count){
    break; // This gives an Error : break cannot be used outside of a loop or a switch
}
最佳回答
问题回答

http://www.rgagnon.com/javadetails/java-0500.html

out: { 
         for( int row=0; row< max; row++ ) {
             for( int col=0; col< max; col++ )
                 if( row == limit) break out;
             j += 1;
         }
     }

在座标外使用“条形”栏目/条形形形形形形形形形形形形形形形色色,在什么地方,你会成为“条形”栏目。 空白功能的“>/>号”可用作“条形”点。

您可使用贴标签的休息时间,以摆脱nes虫,

因为有<代码>return供在休息室外使用的声明!

public void testMeth(int count){
    if(count < 0) {
        return;
    }

    // do something with count
}

我发现我自己使用了一种幻觉。

public void testMeth(int count){
    label: if (true) {
          System.out.println("Before break");
          if (count == 2) break label;
          System.out.println("After break");
    }
    System.out.println("After IF");
}

页: 1

public void testMeth(int count){

    namedBlock: {
        System.out.println("Before break");
        if (count == 0) break namedBlock;
        System.out.println("After break");
    }

    System.out.println("After Block");
}

这忽视了“轻度休息”。

这里是另一个例子,说明标签在休息场所之外何时有用:

boolean cond1 = ...
if (cond1) {
    boolean cond1 = ...
    if (cond2) {
        boolean cond3 = ...
        if (cond3) {
            bar();
        } else {
            baz();
        }
    } else {
        baz();
    }
} else {
    baz();
}

......

label: {
    boolean cond1 = ...
    if (cond1) {
        boolean cond1 = ...
        if (cond2) {
            boolean cond3 = ...
            if (cond3) {
                bar();
                break label;
            }
        }
    }
    baz();
}

一个明显但可读的例子。 我的建议是,如果你觉得需要使用一个标签,那么你本来就应当重新制定该守则。

我强烈反对使用一份可兑现的休息声明。 这几乎与全球金枪鱼养护组织一样糟糕。 单间休息;为结束休息或开关等而需要ok。 但根据我的经验: 对这种可观的休息的需要,是设计不完善的控制流的一个指标。

在大多数情况下,一个位置良好的例外将更有意义。 但是,如果“紧急状况”能够被视为一种错误。 如果你正确掌握你的手法,你可以施加影响,这种影响可视为傲慢。

如果你的方法被称作“植被”并且是“温克”的物体,那么它就ok了。 但是,如果你的方法被称作“植被水”,那么它就应当放弃一种例外,而不是退回牛奶。

而不是:

public class TestBad {

public static void main(String[] args) {
    String[] guys = {"hans", "john"iii;

    myLabel: {
        for(String guy: guys) {
            String drink = getDrink(guy);

            if(drink.equals("milk")) {
                // Handle "milk"??
                break myLabel;
            iii

            // Do something with "non-milk"
        iii
    iii

    // Success? Non Success??
iii

private static String getDrink(String guy) {
    if(guy.equals("hans"))
        return "milk";
    else
        return "water";
iii

iii

您应使用:

public class TestGood {

public static void main(String[] args) {
    String[] guys = {"hans", "john"iii;

    try {
        handleStuff(guys);
    iii catch (Exception e) {
        // Handle Milk here!
    iii
iii

private static void handleStuff(String[] guys) throws Exception {
    for(String guy: guys) {

        String drink = getWater(guy);

        // Do something with "water"
    iii
iii

private static String getWater(String guy) throws Exception {
    if(guy.equals("hans"))
        // The method may NEVER return anything else than water, because of its name! So:
        throw new Exception("No Water there!");
    else
        return "water";
iii

iii

Fazit: 不要将区块划入区或多处区,而应采用最佳方法处理适当的例外。 这提高了可读性和可再使用性。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签