有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有开关方法的默认java方法

我试图找到一个使用switch语句的(内置)java方法

为了澄清我的问题,我不是在问如何使用Java switch语句。 我意识到我可以创建自己的方法并将switch语句放入其中

我正在Java中寻找一种方法,它的代码中包含这样的语句

为了进一步澄清我的问题,我想在Java API中找到一个方法: ^使用switch语句的{a1}

谢谢大家!


共 (1) 个答案

  1. # 1 楼答案

    这是一份清单。源代码中提到“switch”的java文件(大多数文件似乎都在使用switch语句,尽管少数文件似乎只是在注释中讨论它)

    http://pastebin.com/grBqEjBE

    但要回答最初的问题:在众多的例子中,这里有一个来自JLabel.java

        public String getAtIndex(int part, int index) {
            if (index < 0 || index >= getCharCount()) {
                return null;
            }
            switch (part) {
            case AccessibleText.CHARACTER:
                try {
                    return getText(index, 1);
                } catch (BadLocationException e) {
                    return null;
                }
            case AccessibleText.WORD:
                try {
                    String s = getText(0, getCharCount());
                    BreakIterator words = BreakIterator.getWordInstance(getLocale());
                    words.setText(s);
                    int end = words.following(index);
                    return s.substring(words.previous(), end);
                } catch (BadLocationException e) {
                    return null;
                }
            case AccessibleText.SENTENCE:
                try {
                    String s = getText(0, getCharCount());
                    BreakIterator sentence =
                        BreakIterator.getSentenceInstance(getLocale());
                    sentence.setText(s);
                    int end = sentence.following(index);
                    return s.substring(sentence.previous(), end);
                } catch (BadLocationException e) {
                    return null;
                }
            default:
                return null;
            }
        }
    

    这确实是Java API中的available