给你把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应该是不能继承的,
StringBuffer应该可以。
要是我继承一个StringBuffer,然后任务完成。
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();
}
#include<string>using namespace std;class S
{
char mystring[100];public:
S()
{
mystring[0]=0;
}
S(char a[100])
{
strcpy(mystring,a);
}
void S::print()
{
cout<<mystring<<endl;
}
void operator ==(S b);
void operator <(S b);
void operator >(S b);
S operator +(S b);
S operator =(S b);
};
S S::operator =(S b)
{
return strcpy(mystring,b.mystring);
}
S S::operator +(S b)
{
return strcat(mystring,b.mystring);
}
void S::operator >(S b)
{
if(strcmp(mystring,b.mystring)>0)
cout<<"true"<<endl;
else
cout<<"fault"<<endl;
}
void S::operator <(S b)
{
if(strcmp(mystring,b.mystring)<0)
cout<<"true"<<endl;
else
cout<<"fault"<<endl;
}
void S::operator==(S b)
{
if(strcmp(mystring,b.mystring)==0)
cout<<"true"<<endl;
else
cout<<"fault"<<endl;
}
void main()
{
//char x;
S a,b("5778"),c("xjasdhj"),d;
a=b;
b+c;
a.print();
b.print();
c.print();
d.print();
b>c;
b<c;
b==c;
}