【问题标题】:coercion to interface with methods having multiple parameters强制与具有多个参数的方法接口
【发布时间】:2016-02-14 19:02:06
【问题描述】:

假设我有这样的界面

interface IFile {
    void writeFile(String name, byte[] bytes)
    byte[] readFile(String name)
}

如何对该界面进行强制转换? 因为到目前为止该方法不起作用并导致编译异常

def fileCoeImp = { 
    name, bytes -> new File(nane) << bytes,  
    name -> new File(name).getBytes()
} as IFile 

【问题讨论】:

    标签: groovy coercion


    【解决方案1】:

    你需要使用地图:

    def fileCoeImp = [
        writeFile : { name, bytes -> new File(name) << bytes },  
        readFile : { name -> new File(name).getBytes() }
    ] as IFile 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      相关资源
      最近更新 更多