使用switch方法的默认java方法

时间:2013-02-27 03:16:30

标签: java switch-statement

我正在尝试找到一个使用switch语句的(内置)java方法。

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

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

为了进一步澄清我的问题,我想在Java API中找到一个方法: 使用switch语句的http://docs.oracle.com/javase/7/dofucs/api/

谢谢!

1 个答案:

答案 0 :(得分:3)

这是源代码中提到“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