如何做到高级Kotlin强化实战?(三)

高级Kotlin强化实战(二)

          • [2.13 constructor 构造器](#2.13 constructor 构造器)
          • [2.14 Get Set 构造器](#2.14 Get Set 构造器)
          • [2.15 操作符](#2.15 操作符)
          • [2.16 换行](#2.16 换行)

2.13 constructor 构造器
java 复制代码
//Java
public class Utils {
private Utils() {
  }
public static int getScore(int value) {
return 2 * value;
  }
}
kotlin 复制代码
//Kotlin
class Utils private constructor() {
companion object {
fun getScore(value: Int): Int {
return 2 * value
    }
  }
}
// 或者
object Utils {
fun getScore(value: Int): Int {
return 2 * value
  }
}

2.14 Get Set 构造器
java 复制代码
//Java
public class Developer {
private String name;
private int age;
public Developer(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
kotlin 复制代码
//kotlin
data class Developer(val name: String, val age: Int)

2.15 操作符
java 复制代码
//Java
final int andResult = a & b;
final int orResult = a | b;
final int xorResult = a ^ b;
final int rightShift = a >> 2;
final int leftShift = a << 2;
final int unsignedRightShift = a >>> 2;
kotlin 复制代码
//kotlin
val andResult = a and b
val orResult = a or b
val xorResult = a xor b
val rightShift = a shr 2
val leftShift = a shl 2
val unsignedRightShift = a ushr 2

2.16 换行
java 复制代码
//Java
String text = "First Line\n" +
"Second Line\n" +
"Third Line";
kotlin 复制代码
//kotlin
val text = """
|First Line
|Second Line
|Third Line
""".trimMargin()

相关推荐
Blossom.1182 小时前
移动端部署噩梦终结者:动态稀疏视觉Transformer的量化实战
java·人工智能·python·深度学习·算法·机器学习·transformer
AiXed3 小时前
PC微信协议之AES-192-GCM算法
前端·数据库·python
灵光通码3 小时前
神经网络基本概念
python·神经网络
武子康5 小时前
Java-171 Neo4j 备份与恢复 + 预热与执行计划实战
java·开发语言·数据库·性能优化·系统架构·nosql·neo4j
Petrichor_H_5 小时前
DAY 31 文件的规范拆分和写法
python
怪兽20146 小时前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin
ClearLiang6 小时前
Kotlin-协程的挂起与恢复
开发语言·kotlin
彭同学学习日志6 小时前
Kotlin Fragment 按钮跳转报错解决:Unresolved reference ‘floatingActionButton‘
android·开发语言·kotlin
海域云赵从友6 小时前
破解跨境数据传输瓶颈:中国德国高速跨境组网专线与本地化 IP 的协同策略
开发语言·php
咚咚王者6 小时前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python