当前位置:编程学习 > JAVA >>

如何实现一个 字符串类 ??

老师让我自己编写String的一些方法,让后实现一个字符串类,结果我只是把编写的那些方法放在一个类里面了,老师说不可以,他不让用String的原有方法,还要有什么构造函数,谁能给出一些思路啊,解释一下什么意思啊?? string --------------------编程问答-------------------- 不知道你说的抽象,还是你老是说的抽象。
String应该是不能继承的,
StringBuffer应该可以。
要是我继承一个StringBuffer,然后任务完成。 --------------------编程问答-------------------- 主要就是对String的方法自己从新编写,实现一个字符串类,字符串类是怎么实现的??? --------------------编程问答-------------------- 就是重写String类的方法,这个简单,你看那些方法都是来实现什么方法,你就重写这个方法名,在这个方法里自己写要来实现的功能,只要是重写!!! --------------------编程问答-------------------- 看看String的源码就明白了 --------------------编程问答-------------------- 已经重写完了,然后我把这些一堆的函数全放在一个类里面了,老师说不行,他说让我实现的是一个 字符串类~~这我就不太明白怎么弄了,给点思路啊………… --------------------编程问答-------------------- 不让用原生的String类,那就是让你们自己实现一个String类吧?你看原来的怎么写的,就用自己的代码重写一下吧.. --------------------编程问答-------------------- 他的意思可能谁叫你override String方法 --------------------编程问答-------------------- 先去看看api吧,。、。http://www.ostools.net/apidocs/apidoc?api=jdk-zh

源码可以在jdk里面找到...



eclipse上面弄的。 --------------------编程问答-------------------- 给你把java.lang.String简化一下
MyString类:
public class MyString implements MyCharSequence{
/** The value is used for character storage. */
private final char value[];

/** The offset is the first index of the storage that is used. */
private final int offset;

/** The count is the number of characters in the String. */
private final int count;

public MyString() {
this.offset = 0;
this.count = 0;
this.value = new char[0];
}

// Package private constructor which shares value array for speed.
    MyString(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
    }
    
/**
     * Returns the length of this string.
     * The length is equal to the number of <a href="Character.html#unicode">Unicode
     * code units</a> in the string.
     *
     * @return  the length of the sequence of characters represented by this
     *          object.
     */
@Override
    public int length() {
        return count;
    }

/**
     * Returns the <code>char</code> value at the
     * specified index. An index ranges from <code>0</code> to
     * <code>length() - 1</code>. The first <code>char</code> value of the sequence
     * is at index <code>0</code>, the next at index <code>1</code>,
     * and so on, as for array indexing.
     *
     * <p>If the <code>char</code> value specified by the index is a
     * <a href="Character.html#unicode">surrogate</a>, the surrogate
     * value is returned.
     *
     * @param      index   the index of the <code>char</code> value.
     * @return     the <code>char</code> value at the specified index of this string.
     *             The first <code>char</code> value is at index <code>0</code>.
     * @exception  IndexOutOfBoundsException  if the <code>index</code>
     *             argument is negative or not less than the length of this
     *             string.
     */
@Override
    public char charAt(int index) {
        if ((index < 0) || (index >= count)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index + offset];
    }

/**
     * Returns a new string that is a substring of this string. The
     * substring begins at the specified <code>beginIndex</code> and
     * extends to the character at index <code>endIndex - 1</code>.
     * Thus the length of the substring is <code>endIndex-beginIndex</code>.
     * <p>
     * Examples:
     * <blockquote><pre>
     * "hamburger".substring(4, 8) returns "urge"
     * "smiles".substring(1, 5) returns "mile"
     * </pre></blockquote>
     *
     * @param      beginIndex   the beginning index, inclusive.
     * @param      endIndex     the ending index, exclusive.
     * @return     the specified substring.
     * @exception  IndexOutOfBoundsException  if the
     *             <code>beginIndex</code> is negative, or
     *             <code>endIndex</code> is larger than the length of
     *             this <code>String</code> object, or
     *             <code>beginIndex</code> is larger than
     *             <code>endIndex</code>.
     */
    public MyString substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
    throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
    throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
    throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex == count)) ? this :
    new MyString(offset + beginIndex, endIndex - beginIndex, value);
    }

    /**
     * Returns a new character sequence that is a subsequence of this sequence.
     *
     * <p> An invocation of this method of the form
     *
     * <blockquote><pre>
     * str.subSequence(begin, end)</pre></blockquote>
     *
     * behaves in exactly the same way as the invocation
     *
     * <blockquote><pre>
     * str.substring(begin, end)</pre></blockquote>
     *
     * This method is defined so that the <tt>String</tt> class can implement
     * the {@link CharSequence} interface. </p>
     *
     * @param      beginIndex   the begin index, inclusive.
     * @param      endIndex     the end index, exclusive.
     * @return     the specified subsequence.
     *
     * @throws  IndexOutOfBoundsException
     *          if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative,
     *          if <tt>endIndex</tt> is greater than <tt>length()</tt>,
     *          or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt>
     *
     * @since 1.4
     * @spec JSR-51
     */
    public MyCharSequence subSequence(int beginIndex, int endIndex) {
        return this.substring(beginIndex, endIndex);
    }
}

MyCharSequence接口:定义我要生成的字符串类的基本操作
public interface MyCharSequence {


    /**
     * Returns the length of this character sequence.  The length is the number
     * of 16-bit <code>char</code>s in the sequence.</p>
     *
     * @return  the number of <code>char</code>s in this sequence
     */
    int length();

    /**
     * Returns the <code>char</code> value at the specified index.  An index ranges from zero
     * to <tt>length() - 1</tt>.  The first <code>char</code> value of the sequence is at
     * index zero, the next at index one, and so on, as for array
     * indexing. </p>
     *
     * <p>If the <code>char</code> value specified by the index is a
     * <a href="Character.html#unicode">surrogate</a>, the surrogate
     * value is returned.
     *
     * @param   index   the index of the <code>char</code> value to be returned
     *
     * @return  the specified <code>char</code> value
     *
     * @throws  IndexOutOfBoundsException
     *          if the <tt>index</tt> argument is negative or not less than
     *          <tt>length()</tt>
     */
    char charAt(int index);

    /**
     * Returns a new <code>CharSequence</code> that is a subsequence of this sequence.
     * The subsequence starts with the <code>char</code> value at the specified index and
     * ends with the <code>char</code> value at index <tt>end - 1</tt>.  The length
     * (in <code>char</code>s) of the
     * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt>
     * then an empty sequence is returned. </p>
     * 
     * @param   start   the start index, inclusive
     * @param   end     the end index, exclusive
     *
     * @return  the specified subsequence
     *
     * @throws  IndexOutOfBoundsException
     *          if <tt>start</tt> or <tt>end</tt> are negative,
     *          if <tt>end</tt> is greater than <tt>length()</tt>,
     *          or if <tt>start</tt> is greater than <tt>end</tt>
     */
    MyCharSequence subSequence(int start, int end);

    /**
     * Returns a string containing the characters in this sequence in the same
     * order as this sequence.  The length of the string will be the length of
     * this sequence. </p>
     *
     * @return  a string consisting of exactly this sequence of characters
     */
    public String toString();


}

--------------------编程问答-------------------- 意思就是你自己写代码实现String类的功能,比如String类有一个方法toUpperCase(),功能是把字符串转化为大写;你需要自己去实现他,而不是用线程的方法。
--------------------编程问答-------------------- 意思就是你自己写代码实现String类的功能,比如String类有一个方法toUpperCase(),功能是把字符串转化为大写;你需要自己去实现他,而不是用现成的方法。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,