赞
踩
使用 infix
修饰的函数,叫 中缀函数。可 省去 .
和 ()
, 直接 通过 名 调用。
比如,10 shl 2
位左移操作shl
就是 中缀函数,等同于 10.shl(2)
。
声明格式
infix fun methodName(argName: String)
即:类成员函数 或 扩展函数,增加 infix
修饰即可。
vararg
),也不能有 默认值。查看 Int
位操作 相关函数源码,可知 都是 中缀函数:
public class Int private constructor() : Number(), Comparable<Int> {
public infix fun shl(bitCount: Int): Int
public infix fun shr(bitCount: Int): Int
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。