Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

kotlin - Class with functions that extends more than one implementation of the same data structure. Can be done?

I'm studying Data Structures in my CS course. University professor assigned us a project where we have to implement every data structure from 0 without using the already one provided by the programming language we choosed to use.

All of this is required for pass his lab part of the exam:

  • For every data structure we have to develop more than one known implementations.

     An istance : For the List data structure
                  We have to develop ArrayList, LinkedList and DoubleLinkedList in separate 
                  classes and packages.
    
  • We have to follow the known formal specifications.

     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
    

I completed those ones, but

I need to put "services functions" in a separated class/file for every Data Structure and it should work for every class(implementation) of them.

then following the istances above:

I have to develop a class/file named "ListServices" that should "extend" every class like: MyArrayList MyLinkedList MyDoubleLinkedList

For Services I mean "not needed" functions for the data structures like:

printList() //it prints the whole list

goToList(position : Int) //goes to the position using Integers

sortList(whichsort : String, order : String) //whichsort = quicksort,mergesort and order ascending/descending

purgeList() //it deletes all the "dupled" elements(or the more than 2 occurances per element)

Usually I will define those in every class that I named above like this:

class MyArrayList<T>{
.
.
.
fun printList(){}
}
class MyLinkedList<T>{
.
.
.
fun printList(){}
}
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

}

Whatever the implementation I use, printList() will use only the data structure operators and nothing else. The code of printList() is the same for every List Implementation. I cannot place Services Function there!

I need to "move out" printlist()[and others services functions] from the List Implementations to another class or file.

How can I fullfill this request with Kotlin?

Thanks you all.

P.S. = I'm not an english native person, sorry if I'm not clear as you expect, feel free to ask more and I will reply!

question from:https://stackoverflow.com/questions/65866496/class-with-functions-that-extends-more-than-one-implementation-of-the-same-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you should create an inteface and extend it in all your clases

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

Or if you dont want to create a relationship between these classes, you can use extension functions

object ListServices {
    fun <T> myArrayList<T>.printList() {}
    fun <T> myLinkedList<T>.printList() {}
    fun <T> myDoubleLinkedList<T>.printList() {}
}

T is a type parameter, you must declare it before the name of the function. If your print function doesnt care about this type, you can use the star-projection:

class myArrayList<T>(var foo: T)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...