2016-09-18 92 views
0

我有接口無法解析參考方法

interface IIMSIdentifiable { 
    fun setImsId(id : String) 
    fun getImsId() : String 
} 

interface IIMSConsumer : IIMSIdentifiable { 
    fun consumeAsync(message : GRLMessage) 
} 

而且我有一個包含對象與IIMSConsumer

class IMSObject<IIMSConsumer> : Thread { 
    constructor(component : IIMSConsumer) { 
     obj = component 
     // IMSContext.instance.registerObject(this) // hm type mismatch 
    } 

    val objectMessageQueue = LinkedBlockingDeque<GRLMessage>() 
    val obj : IIMSConsumer 
    var isRunning = true 

    override fun run() { 
     while(isRunning) { 
      processMessages() 
     } 
    } 

    fun stopProcessing() { 
     isRunning = false 
    } 

    fun processMessages() { 
     objectMessageQueue.forEach { 
      obj.consumeAsync(it) 
     } 
    } 

    fun getObjectId() : String { 
     return obj.getImsId() 
    } 
} 

一類,但它無法解析引用

fun processMessages() { 
    objectMessageQueue.forEach { 
     obj.consumeAsync(it) // cannot resolve reference !!! 
    } 
} 

fun getObjectId() : String { 
    return obj.getImsId() // cannot resolve reference !!! 
} 

問題是什麼?奇怪的是足夠多的它並沒有對進口儘管位於不同的包問

com.lapots.game.journey.ims.domain.IMSObject 
com.lapots.game.journey.ims.api.IIMSConsumer 

我試着用unresolved reference

interface IConsumer { 
    fun consume() : String 
} 

class Generic<IConsumer>(val consumer : IConsumer) { 
    fun invoke() { 
     print(consumer.consume()) // unresolved reference 
    } 

} 

fun main(args: Array<String>) { 
    val consumer = object : IConsumer { 
     override fun consume() : String { 
      return "I consume" 
     } 
    } 
    val generic = Generic<IConsumer>(consumer) 
    generic.invoke() 
} 

回答

2

class Generic<IConsumer>(val consumer : IConsumer) {

你是測試在簡單的東西,並得到了同樣的錯誤用名爲IConsumer的通用類型參數創建類Generic。這種類型的參數將跟隨你,類中定義的接口,所以你實際上說的是:

class Generic<IConsumer : Any>(val consumer : Any) {

這就是爲什麼它不能解析的方法,因爲泛型參數只能被解釋爲Any

要修改或更改類型參數以具有適當的名稱並綁定(class Generic<T : IConsumer>(val consumer : T) {)或完全刪除泛型