如何做到高级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()

相关推荐
我命由我1234511 分钟前
Java 开发 - CountDownLatch 不需要手动关闭
android·java·开发语言·jvm·kotlin·android studio·android-studio
谭欣辰31 分钟前
详细讲解 C++ 状压 DP
开发语言·c++·动态规划
weixin_5689960633 分钟前
c++如何实现日志文件的异步落盘功能_基于无锁队列方案【附代码】
jvm·数据库·python
chaofan98039 分钟前
GPT-5.5 全压力测试:为什么 API 聚合调度是解决“首字延迟”的技术关键?
开发语言·人工智能·python·gpt·自动化·api
才兄说40 分钟前
机器人二次开发机器人动作定制?定制化舞蹈
python
William_wL_43 分钟前
【C++】stack和queue的使用和实现(附加deque的简单介绍)
开发语言·c++
曲幽1 小时前
FastAPI配置管理避坑指南:从硬编码到 .env 与 pydantic_settings 类,连路由用法都给你捋清楚
python·fastapi·web·settings·config·pydantic·.env·dotenv·.env.prod
hhb_6181 小时前
D架构底层调度与性能优化实践指南
开发语言
淘矿人1 小时前
2026年4月-DeepSeek V4 vs GPT-5.5深度对比测评:weelinking一键切换实测
服务器·数据库·人工智能·python·gpt·学习·php
秋91 小时前
Java AI编程工具全景解析:功能、收费与工单系统实战指南
java·开发语言·ai编程