Class 使用扩展同一数据结构的多个实现的函数初始化。可以吗?

Class 使用扩展同一数据结构的多个实现的函数初始化。可以吗?,class,kotlin,data-structures,Class,Kotlin,Data Structures,我在CS课程中学习数据结构。 大学教授给我们分配了一个项目,在这个项目中,我们必须实现0中的每个数据结构,而不使用我们选择使用的编程语言提供的数据结构 所有这些都是通过实验室部分考试所必需的: 对于每个数据结构,我们必须开发多个已知的实现 An istance : For the List data structure We have to develop ArrayList, LinkedList and DoubleLinkedList in separate

我在CS课程中学习数据结构。 大学教授给我们分配了一个项目,在这个项目中,我们必须实现0中的每个数据结构,而不使用我们选择使用的编程语言提供的数据结构

所有这些都是通过实验室部分考试所必需的:

  • 对于每个数据结构,我们必须开发多个已知的实现

     An istance : For the List data structure
                  We have to develop ArrayList, LinkedList and DoubleLinkedList in separate 
                  classes and packages.
    
  • 我们必须遵循已知的正式规范

     An istance : For the List class
                  We can only declare "data structure releated" members and attributes
                  Like create,delete,head,isEmpty,isLast,next,previous,insert,write,read,remove
    
我完成了那些,但是

我需要为每个数据结构将“服务函数”放在一个单独的类/文件中,并且它应该适用于它们的每个类(实现)

然后按照上面的步骤:

我必须开发一个名为“ListServices”的类/文件,它应该“扩展”每个类,如: MyArrayList 我的链接列表 MyDoubleLinkedList

对于服务,我指的是数据结构的“不需要”功能,如:

printList()//它打印整个列表

goToList(position:Int)//使用整数转到位置

sortList(whichsort:String,order:String)//whichsort=quicksort,mergesort和order升序/降序

purgeList()//它删除所有的“重复”元素(或每个元素出现2次以上)

通常,我会在我上面命名的每个类中定义它们,如下所示:

class MyArrayList<T>{
.
.
.
fun printList(){}
}
类MyArrayList{
.
.
.
趣味打印列表(){}
}
类MyLinkedList{
.
.
.
趣味打印列表(){}
}
类MyDoubleLinkedList{
.
.
.
趣味打印列表(){}
}
将edu.uni.lists.MyArrayList导入为MyList//或任何其他我将交换而不是MyArrayList的实现
主要内容(){
val l1=MyList()
l1.create()//初始化
if(l1.isEmpty())
println(“列表为空”)
l1.插入(“11”,l1.head())
l1.插入(“10”,l1.head())
l1.插入(“9”,l1.head())
l1.插入(“8”,l1.head())
l1.插入(“7”,l1.head())
l1.插入(“69”,l1.head())
l1.printList()//应打印整个列表
}
无论我使用什么实现,printList()将只使用数据结构操作符,而不使用其他任何东西。 printList()的代码对于每个列表实现都是相同的。 我不能把服务功能放在那里

我需要将printlist()[和其他服务函数]从列表实现“移出”到另一个类或文件

我如何用Kotlin填写此请求

谢谢大家

我不是英国人,对不起,如果我不清楚你所期望的,请随意问更多,我会回答

您应该创建一个类,并在所有类中扩展它

interface ListServices {
    fun printList()
}
class myArrayList<T> : ListServices {}
class myLinkedList<T> : ListServices {}
class myDoubleLinkedList<T> : ListServices {}

在Kotlin和大多数其他面向对象语言中,类名应该总是以大写字母开头,仅供参考。是的,我知道,我有那些用大写字母命名的类的意大利语名称,当然不是Kotlin上的关键字。我匆忙地添加了“我的”,因为我害怕把你和kotlin内置的混淆。答案正确!谢谢。我将尝试这些,并了解其中的差异。第二个解决方案符合我的要求。你能补充更多关于为什么我需要放在乐趣之后的信息吗?我不明白,谢谢你!我不知道为什么,但我忽略了Kotlin参考中的这一部分。
class MyDoubleLinkedList<T>{
.
.
.
fun printList(){}
}
import edu.uni.lists.MyArrayList as MyList //or whatever other implementation I will interchange instead of MyArrayList

fun main() {
        val l1 = MyList<String> ()
        l1.create() //init
        if(l1.isEmpty())
            println("List is empty")

        l1.insert("11",l1.head())
        l1.insert("10",l1.head())
        l1.insert("9",l1.head())
        l1.insert("8",l1.head())
        l1.insert("7",l1.head())
        l1.insert("69",l1.head())
        l1.printList() //should print the whole list

}
interface ListServices {
    fun printList()
}
class myArrayList<T> : ListServices {}
class myLinkedList<T> : ListServices {}
class myDoubleLinkedList<T> : ListServices {}
object ListServices {
    fun <T> myArrayList<T>.printList() {}
    fun <T> myLinkedList<T>.printList() {}
    fun <T> myDoubleLinkedList<T>.printList() {}
}
class myArrayList<T>(var foo: T)

object ListServices {
    fun myArrayList<*>.printList() {
        // here foo type is Any?
    }
}