日期:2014-05-19  浏览次数:20837 次

我在读一段原码的时候,看到一个问题代码。
我在读AliceBot(Alice是一个聊天机器人)的源代码,在读到Request.java的时候,看到这样一个方法,额,还是先把代码贴上来吧。
Java code

/*
Copyleft (C) 2005 H?lio Perroni Filho
xperroni@yahoo.com
ICQ: 2490863

This file is part of ChatterBean.

ChatterBean is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

ChatterBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with ChatterBean (look at the Documents/ directory); if not, either write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA, or visit (http://www.gnu.org/licenses/gpl.txt).
 */

package bitoflife.chatterbean.text;

import java.util.Arrays;

public class Request {
    /*
     * Attributes
     */

    private Sentence[] sentences;
    private String original;

    /*
     * Constructor
     */

    public Request() {
    }

    public Request(String original) {
        this.original = original;
    }

    public Request(String original, Sentence... sentences)// 可变参数个数
    {
        this.original = original;
        this.sentences = sentences;
    }

    /*
     * Methods
     */

    public boolean empty() {
        return (sentences == null || sentences.length == 0);
    }

    /**
     * 比较相等必须sentences 和 original 2个变量都要相等。
     */
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof Request))
            return false;

        Request compared = (Request) obj;
        return original.equals(compared.original)
                && Arrays.equals(sentences, compared.sentences);
    }

    
    public Sentence lastSentence(int index) {   //////问题在这里???
        return sentences[sentences.length - (1 + index)];
    } 

    public String toString() {
        return original;
    }

    public String trimOriginal() {
        return original.trim();
    }

    /*
     * Properties
     */

    public String getOriginal() {
        return original;
    }

    public void setOriginal(String original) {
        this.original = original;
    }

    public Sentence[] getSentences() {
        return sentences;
    }

    public Sentence getSentences(int index) {
        return sentences[index];
    }

    public void setSentences(Sentence[] sentences) {
        this.sentences = sentences;
    }
}


上面的lastSentence这个方法就是取数组的最后一个元素啊(我选中该方法,按Ctrl+Alt+h,查看了所有调用到该方法的地方,发现穿进来的index参数都是0),要是我,我会这样写:
Java code

public Sentence lastSentence() {   
        return sentences[sentences.length - 1];
    } 


我觉得我应该自信的认为这个外国人在卖萌,但为了保险期间,我还是问一下各位老师:你们有这样写的吗,这样写有啥用意吗?

------解决方案--------------------
首先不是老师,不过按照你说的,我写的话肯定是你的写法,不过他写有他的道理,他的方法可以取到任意位置的,他任意位置的最后一个,可能被别的方法调用哦。
------解决方案--------------------
他这个可以取到任意位置的值,如果现在有这样的需求,你岂不是还得自己写一个方法
------解决方案--------------------
探讨
他这个可以取到任意位置的值,如果现在有这样的需求,你岂不是还得自己写一个方法

------解决方案--------------------
探讨

引用:
他这个可以取到任意位置的值,如果现在有这样的需求,你岂不是还得自己写一个方法

如果是任意取值,方法应该是 getSentence(int index),lastSentence就是取最后一个。。