在 if 语句中使用 return 时有什么作用? [英] What does return do when used inside an if statement?

查看:33
本文介绍了在 if 语句中使用 return 时有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码中 if 语句中的 return 有什么作用?

What does the return inside the if statements do in the following code?

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;
}

它是把我们带出 if 语句并继续下一条语句,还是带我们离开方法 startElement?

Does it takes us out of the if statement and proceeds to next statement or it takes us out of the method startElement?

推荐答案

上面代码中的返回值会带你跳出方法.

The returns in the above code will take you out of the method.

这篇关于在 if 语句中使用 return 时有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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