String 链表可变字符串继承vs委派

String 链表可变字符串继承vs委派,string,inheritance,linked-list,mutable,delegation,String,Inheritance,Linked List,Mutable,Delegation,我有一个链表问题,这个问题源于我即将发布的作业提示。这可能有助于回答以下问题: 细节 Java字符串类是不可变的(不能更改内容)。这有时会妨碍您对字符串的处理(即对现有字符串进行更改)。因此,对于这个赋值,您将创建一个名为MutableString的类。注意/免责声明:Java API确实有一个类StringBuffer,它是可变的,但出于本赋值的目的,我们将假装不知道;-) 必须使用链表来表示字符串的字符(必须编写 链接列表类)。LinkedList类应该包含更新和修改列表的基本操作。记住这一

我有一个链表问题,这个问题源于我即将发布的作业提示。这可能有助于回答以下问题:

细节

Java字符串类是不可变的(不能更改内容)。这有时会妨碍您对字符串的处理(即对现有字符串进行更改)。因此,对于这个赋值,您将创建一个名为MutableString的类。注意/免责声明:Java API确实有一个类StringBuffer,它是可变的,但出于本赋值的目的,我们将假装不知道;-)

必须使用链表来表示字符串的字符(必须编写 链接列表类)。LinkedList类应该包含更新和修改列表的基本操作。记住这一点,在JavaAPI中实现List接口。对于您认为对LinkedList的功能不必要的方法,将这些方法存根并抛出一个异常,该异常包含有关调用了什么方法以及该方法尚未实现的信息。注意:确保LinkedList类不执行任何特定于可变字符串的操作。您可以在作业中随意使用以下已删除的LinkedList类。它可能不包含您需要的所有方法,但它确实包含来自JavaAPI的列表接口的方法

MutableString必须尽可能利用LinkedList行为(方法)(不要在MutableString中编写与LinkedList类中的某些操作相同的代码)。因此,您的可变字符串将包含一个引用LinkedList对象的字段。顺便说一句,这个概念被称为委托,这是软件工程和设计中一个非常重要的概念。不要设计可变字符串,使其从LinkedList继承

MutableString的每个节点都应该包含一个字符——请注意,这并不意味着对节点类中的数据的引用应该/必须是Character类型——这将使LinkedList类特定于MutableString,在本例中我们不希望出现这种情况

**我不理解的部分是上面第2段中关于继承链表类的内容。我总是习惯于编写公共类MutableString扩展LinkedList之类的东西,但我们不能这样做,我真的不知道如何在不继承类的情况下开始编写这个MutableString类。帮助解释如何做到这一点和区别将是可怕的。谢谢

我不太擅长链表,所以这个问题很简单:)
**

它说你应该使用委托而不是继承

有关委派和继承之间的区别,请查看。并举例说明

An object of the MutableString class contains the following operations/behaviors -- which means the class itself must contain the following methods:

Character charAt(int index): returns the Character at the specified index in the string -- if the index lies outside the string, throw a MutableStringIndexOutOfBoundsException (you must write this class)

void set(int index, Character ch): replaces the existing Character at the specified location -- if the index lies outside the string throw a MutableStringIndexOutOfBoundsException

void add(int index, Character ch): creates a new spot in the list for the Character at the index specified -- if the index lies outside the string throw a MutableStringIndexOutOfBoundsException

Character remove(int index): removes the Character at specified index -- if the index lies outside the string, throw a MutableStringIndexOutOfBoundsException

boolean remove(Character ch): removes the first occurrence (starting from the beginning of the MutableString) of the Character -- if the Character is not found, return false

boolean removeAll(Character ch): removes all occurrences of the specified Character -- if Character is not found, return false

void toUpper(): converts the current string to all upper case

void toLower(): converts the current string to all lower case

MutableString substring(int start, int finish): returns a string starting at the Character specified by start and concluding with the Character specified by finish -- if start or finish is outside the the string, throw a MutableStringIndexOutOfBoundsException

char [] toCharArray(): returns a char array containing all the characters in the string -- be sure and handle all cases

int length(): reports/returns the length of the string

String toString(): returns a String containing all the Characters

String toReverseString(): returns a String containing all the Characters in reverse order -- you must utilize recursion to accomplish this task

int compareTo(MutableString that): allows comparison of two MutableString objects -- this implies you will implement the Comparable interface for your MutableString class

void sort(): alphabetizes the letters in case-insensitive fashion in ascending order -- you must write the code for this sort (no API calls are allowed)