Kotlin-类

构造函数

Java

java 复制代码
final File file = new File("file.txt");

Kotlin

Kotlin 复制代码
val file = File("file.txt")

Java

java 复制代码
public final class User {
}

Kotlin

Kotlin 复制代码
class User

公开类

Java

java 复制代码
public class User {
}

Kotlin

Kotlin 复制代码
open class User

属性类

Java

java 复制代码
final class User {
     private final String name;

     public User(String name) {
         this.name = name;
     }

     public String getName() {
         return name;
     }
 }

Kotlin

Kotlin 复制代码
class User(val name: String)

有参数的构造函数

Java

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

Kotlin 复制代码
class User(var name: String)

构造函数中的可选参数

Java

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

Kotlin 复制代码
class User(var name: String, var lastName: String = "")

对类中参数赋值

Java

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

Kotlin 复制代码
class Document{
    var id : String = "00x"
        set(value) {
            if(value.isNotEmpty())  field = value
        }
}

抽象类

Java

java 复制代码
public abstract class Document{
   public abstract int calculateSize();
}

public class Photo extends Document{
    @Override
    public int calculateSize() {

    }
}

Kotlin

Kotlin 复制代码
abstract class Document {
    abstract fun calculateSize(): Int
}

class Photo : Document() {
    override fun calculateSize(): Int {

    }
}

单例类

Java

java 复制代码
public class Document {
   private static final Document INSTANCE = new Document();

   public static Document getInstance(){
       return INSTANCE;
   }

 }
 

Kotlin

Kotlin 复制代码
object Document {

}

扩展类

Java

java 复制代码
public class ByteArrayUtils {
      public static String toHexString(byte[] data) {

      }
  }

  final byte[] dummyData = new byte[10];
  final String hexValue = ByteArrayUtils.toHexString(dummyData);

Kotlin

Kotlin 复制代码
fun ByteArray.toHex() : String {

}

val dummyData = byteArrayOf()
val hexValue = dummyData.toHex()

内部类

Java

java 复制代码
public class Documment {

    class InnerClass {

    }

}

Kotlin

Kotlin 复制代码
class Document {
    inner class InnerClass
}

嵌套类

Java

java 复制代码
public class Documment {

    public static class InnerClass {

    }

}

Kotlin

Kotlin 复制代码
class Document {

    class InnerClass

}

接口

Java

java 复制代码
public interface Printable {
    void print();
}

public class Document implements Printable {
    @Override
    public void print() {

    }
}

Kotlin

Kotlin 复制代码
interface Printable{
    fun print()
}

class Document : Printable{
    override fun print() {

    }
}
相关推荐
alexhilton5 小时前
灵活、现代的Android应用架构:完整分步指南
android·kotlin·android jetpack
雨白11 小时前
初识协程: 为什么需要它以及如何启动第一个协程
android·kotlin
heeheeai12 小时前
Kotlinx Serialization 指南
kotlin·序列化
用户0918 小时前
MVI架构如何改变Android开发模式
android·面试·kotlin
小孔龙18 小时前
K2 编译器 - Symbol(符号系统)
kotlin·编译器
phoneixsky1 天前
Kotlin的各种上下文Receiver,到底怎么个事
kotlin
heeheeai1 天前
okhttp使用指南
okhttp·kotlin·教程
Monkey-旭2 天前
Android 注解完全指南:从基础概念到自定义实战
android·java·kotlin·注解·annotation
alexhilton2 天前
如何构建Android应用:深入探讨原则而非规则
android·kotlin·android jetpack
TeleostNaCl2 天前
SMBJ 简单使用指南 实现在 Java/Android 程序中访问 SMB 服务器
android·java·运维·服务器·经验分享·kotlin