本文采用與 Java 語法作對比的方式羅列并介紹 Kotlin 類的用法鞭光。依然是類似《Kotlin 帶你飛 ? 語法篇 ? 基礎》的套路视事,希望能對看到這篇文章的您有所幫助吧绽昏。
相關文章:
《Kotlin 帶你飛》
《Kotlin 帶你飛 ? 語法篇 ? 基礎》
《Kotlin 帶你飛 ? 語法篇 ? 函數》
《Kotlin 帶你飛 ? 實戰(zhàn)篇》
相關 Github 源碼
版權聲明:本文為 frendy 原創(chuàng)文章脑融,可以隨意轉載虏劲,但請務必在明確位置注明出處阅虫。
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() {
}
}
愿大家都能飛得更高演闭、飛得更開心...嗯,開心很重要...