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

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

问题描述

有人告诉我,在OOP语言中使用Break and Continue标签不是OOP编程风格.您能否详细说明问题的原因和原因?

I was told that using break and continue labels in an OOP language is not OOP programming style. Can you explain in detail why and what is the 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.

break/continue仅允许您跳至外部语句,这意味着您无法遍历代码.因此,您留在同一个方法对象中,因此它与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.

无论如何,说中断和继续不是面向对象的,这是无稽之谈.我们也许可以讨论它们对可读性的影响,但仅此而已.

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(例如Java,C#)中使用中断/继续标签是一种不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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