官术网_书友最值得收藏!

Operators

Operators are functions that use a symbolic name. In Kotlin, many built-in operators are actually function calls. For example, array access is a real function:

    val array = arrayOf(1, 2, 3) 
    val element = array[0] 

In this example, the [0] operation is translated into a call to the get(index: Int) function defined on the Array class.

Many operators are predefined in Kotlin, just like they are in most other languages, and most operators tend to be combined with the infix style. This is immediately familiar in the guise of binary operators on numbers.

Although Kotlin treats operations on basic types as functions, they are compiled to the appropriate byte code operations to avoid function overhead and ensure maximum performance.

Often operators are preferred over real names if the operators are already familiar to the users. In fields such as mathematics or physics, where operators are routinely used, it would be natural to also use operations in code where appropriate. For example, in the case of matrices, using the + character for matrix addition feels more natural than using the words add or plus. It is also easier to read when the parentheses are omitted:

    val m: Matrix =  
    val n: Matrix =  
    val p = n + q 

As you can see, creating a new p matrix by adding together two other matrices, n and m, by using the + syntax is succinct and readable.

主站蜘蛛池模板: 元氏县| 西青区| 九龙坡区| 章丘市| 寿宁县| 公安县| 安化县| 南丹县| 沁阳市| 诏安县| 淮滨县| 三亚市| 成都市| 井研县| 永善县| 通海县| 锡林郭勒盟| 汝城县| 泰宁县| 崇义县| 隆林| 湘西| 犍为县| 九江市| 民勤县| 泗水县| 阿勒泰市| 汉中市| 峡江县| 阿克苏市| 西乡县| 陇南市| 巴林右旗| 泸水县| 满洲里市| 长岛县| 河池市| 仲巴县| 中超| 磐安县| 崇仁县|