我只想将通用值从生产者类传递到消费者类。如果我尝试为消费者创建对象,那么我必须选择任何特定类型,例如字符串。当我使用对象/伴侣对象作为消费者时,情况相同。
class Producer<T> {
fun send(list: List<T>) {
/*
1. I dont want to create object which belongs to any particular type say string
val consumer = Consumer<String>()
Consumer.receive(list)
2. if i try to create companion object, then i have to give the generic type.
*/
}
}
class Consumer<T> {
val map: MutableMap<String, List<T>> = mutableMapOf()
fun receive(list: List<T>) {
map.put("key", list)
}
}