欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > 如何做到高级Kotlin强化实战?(三)

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

2025/9/18 14:44:14 来源:https://blog.csdn.net/weixin_56715699/article/details/140403879  浏览:    关键词:如何做到高级Kotlin强化实战?(三)

高级Kotlin强化实战(二)

          • 2.13 constructor 构造器
          • 2.14 Get Set 构造器
          • 2.15 操作符
          • 2.16 换行


2.13 constructor 构造器
//Java
public class Utils {
private Utils() {}
public static int getScore(int value) {
return 2 * value;}
}
//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
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
data class Developer(val name: String, val age: Int)

2.15 操作符
//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
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
String text = "First Line\n" +
"Second Line\n" +
"Third Line";
//kotlin
val text = """
|First Line
|Second Line
|Third Line
""".trimMargin()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词