为什么在OOP中使用break / continue标签是不好的做法(例如Java,C#) [英] Why it is a bad practice to use break/continue labels in OOP (e.g. Java, C#)

查看:137
本文介绍了为什么在OOP中使用break / continue标签是不好的做法(例如Java,C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知在OOP语言中使用break和continue标签不是OOP编程风格。你能详细解释为什么和什么是问题?
诀窍在于这个标签字。我的意思是标记为break / continue。

I was told that using break and continue labels in OOP language is not OOP programming style. Can you explain in detail why and what is a problem? The trick was with this label word. I meant labeled break/continue.

class BreakWithLabelDemo {
    public static void main(String[] args) {

        int[][] arrayOfInts = { 
            { 32, 87, 3, 589 },
            { 12, 1076, 2000, 8 },
            { 622, 127, 77, 955 }
        };
        int searchfor = 12;

        int i;
        int j = 0;
        boolean foundIt = false;

    search:
        for (i = 0; i < arrayOfInts.length; i++) {
            for (j = 0; j < arrayOfInts[i].length;
                 j++) {
                if (arrayOfInts[i][j] == searchfor) {
                    foundIt = true;
                    break search;
                }
            }
        }

        if (foundIt) {
            System.out.println("Found " + searchfor +
                               " at " + i + ", " + j);
        } else {
            System.out.println(searchfor +
                               " not in the array");
        }
    }
}

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

推荐答案

告诉你的人可能意味着break和continue是分支语句,如goto,这是命令式编程的一种机制。

The person who told you that would probably means that break and continue are branching statements like goto which are one mechanism of imperative programming.

中断/继续只允许跳转到外部语句,这意味着你无法在代码中到处走。所以你留在同一个方法对象中,所以它与OOP不相容。

A break/continue only allow you to jump to an outer statement, which means that you cannot go everywhere in the code. So you stay in the same method object, so it's not incompatible with OOP.

无论如何,说断点和继续不是OOP是没有意义的。我们可以讨论它们对可读性的影响,但这就是全部。

Anyway, saying that break and continue are not OOP is a non-sense. We can discuss about their impact on the readibility maybe but that's all.

这篇关于为什么在OOP中使用break / continue标签是不好的做法(例如Java,C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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