在if语句中使用return时会做什么?

uni*_*pri 18 java if-statement return

if语句中的返回在以下代码中做了什么?

public void startElement(String namespaceURI, String localName,String qName, 
                                         Attributes atts) throws SAXException
{
    depth++;
    if (localName.equals("channel"))
    {
        currentstate = 0;
        return;
    }
    if (localName.equals("image"))
    {
        // record our feed data - you temporarily stored it in the item :)
        _feed.setTitle(_item.getTitle());
        _feed.setPubDate(_item.getPubDate());
    }
    if (localName.equals("item"))
    {
        // create a new item
        _item = new RSSItem();
        return;
    }
    if (localName.equals("title"))
    {
        currentstate = RSS_TITLE;
        return;
    }
    if (localName.equals("description"))
    {
        currentstate = RSS_DESCRIPTION;
        return;
    }
    if (localName.equals("link"))
    {
        currentstate = RSS_LINK;
        return;
    }
    if (localName.equals("category"))
    {
        currentstate = RSS_CATEGORY;
        return;
    }
    if (localName.equals("pubDate"))
    {
        currentstate = RSS_PUBDATE;
        return;
    }
    // if you don't explicitly handle the element, make sure you don't wind 
           // up erroneously storing a newline or other bogus data into one of our 
           // existing elements
    currentstate = 0;
}
Run Code Online (Sandbox Code Playgroud)

它是否会将我们从if语句中移出并继续执行下一个语句,或者它将我们带出startElement方法?

Sco*_*ott 16

上面代码中的返回将使您退出该方法.

  • 我还注意到它在执行返回的行中执行此操作.该行下的代码都没有执行 (3认同)

mth*_*pvg 10

它完成了方法,因此它下面的代码不会被执行.


eMi*_*eMi 9

它是否会将我们从if语句中移出并继续执行下一个语句,或者它将我们带出startElement方法?

它将您带出方法.. return语句终止函数的执行