本文采用与 Java 语法作对比的方式罗列并介绍 Kotlin 类的用法。依然是类似《Kotlin 带你飞 • 语法篇 • 基础》的套路,希望能对看到这篇文章的您有所帮助吧。
相关文章:
《Kotlin 带你飞》
《Kotlin 带你飞 • 语法篇 • 基础》
《Kotlin 带你飞 • 语法篇 • 函数》
《Kotlin 带你飞 • 实战篇》
相关 Github 源码
版权声明:本文为 frendy 原创文章,可以随意转载,但请务必在明确位置注明出处。
Constructor Call
Java
final File file = new File("file.txt");
Kotlin
val file = File("file.txt")
Class
Java
public final class User { }
Kotlin
class User
Open Class
Java
public class User { }
Kotlin
open class User
Final Attributes
Java
final class User {
private final String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Kotlin
class User(val name: String)
Primary Constructor
Java
final class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Kotlin
class User(var name: String)
Optional Arguments in Constructors
Java
final class User {
private String name;
private String lastName;
public User(String name) {
this(name, "");
}
public User(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}
// And Getters & Setters
}
Kotlin
class User(var name: String, var lastName: String = "")
Properties
Java
public class Document {
private String id = "00x";
public String getId() {
return id;
}
public void setId(String id) {
if(id != null && !id.isEmpty()) {
this.id = id;
}
}
}
Kotlin
class Document{
var id : String = "00x"
set(value) {
if(value.isNotEmpty()) field = value
}
}
Abstract Class
Java
public abstract class Document{
public abstract int calculateSize();
}
public class Photo extends Document{
@Override
public int calculateSize() {
}
}
Kotlin
abstract class Document {
abstract fun calculateSize(): Int
}
class Photo : Document() {
override fun calculateSize(): Int {
}
}
Singleton
Java
public class Document {
private static final Document INSTANCE = new Document();
public static Document getInstance(){
return INSTANCE;
}
}
Kotlin
object Document {
}
Extensions
Java
public class ByteArrayUtils {
public static String toHexString(byte[] data) {
}
}
final byte[] dummyData = new byte[10];
final String hexValue = ByteArrayUtils.toHexString(dummyData);
Kotlin
fun ByteArray.toHex() : String {
}
val dummyData = byteArrayOf()
val hexValue = dummyData.toHex()
Inner Class
Java
public class Documment {
class InnerClass {
}
}
Kotlin
class Document {
inner class InnerClass
}
Nested Class
Java
public class Documment {
public static class InnerClass {
}
}
Kotlin
class Document {
class InnerClass
}
Interface
Java
public interface Printable {
void print();
}
public class Document implements Printable {
@Override
public void print() {
}
}
Kotlin
interface Printable{
fun print()
}
class Document : Printable {
override fun print() {
}
}
愿大家都能飞得更高、飞得更开心...嗯,开心很重要...