S
S
syxme2020-09-17 16:28:22
OOP
syxme, 2020-09-17 16:28:22

Generic class, array of types, how to call array processing by type method?

There is an interface

interface IEvents{
  fun onDeviceChange(index: Int)
  fun onDeviceAdd(index: Int)
}

I need to make method calls on a list.
How I present it:

class Obsr<T>() {
  var list: Collection<T> = mutableListOf()
  fun callAll(){
    for (itm in list){
      itm{метод который я вызываю через класс}
    }
  }
}
var events:Obsr<IEvents> = Obsr()
//Наполняем массив
//....
events.onDeviceChange(1) // вызывается функция callAll и проходится по массиву методом onDeviceChange
//...
events.onDeviceAdd(1) // вызывается функция callAll и проходится по массиву методом onDeviceAdd

Is it possible to write such a generic class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2020-09-18
@syxme

No way. You can, of course, not this crappy design.
Write a normal event bus with annotations and you will be happy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question