Kotlin 带你飞 • 语法篇 • 函数


本文采用与 Java 语法作对比的方式罗列并介绍 Kotlin 的函数用法。依然是类似《Kotlin 带你飞 • 语法篇 • 基础》的套路,希望能对看到这篇文章的您有所帮助吧。

相关文章:
Kotlin 带你飞
Kotlin 带你飞 • 语法篇 • 基础
Kotlin 带你飞 • 实战篇

相关 Github 源码
简书

版权声明:本文为 frendy 原创文章,可以随意转载,但请务必在明确位置注明出处。


Basic Function

Java

public void hello() {
  System.out.print("Hello World!");
}

Kotlin

fun hello() {
    println("Hello World!")
}

Arguments

Java

public void hello(String name){
  System.out.print("Hello " + name + "!");
}

Kotlin

fun hello(name: String) {
    println("Hello $name!")
}

Default Values

Java

public void hello(String name) {
  if (name == null) {
    name = "World";
  }

  System.out.print("Hello " + name + "!");
}

Kotlin

fun hello(name: String = "World") {
    println("Hello $name!")
}

Return

Java

public boolean hasItems() {
  return true;
}

Kotlin

fun hasItems() : Boolean {
    return true
}

Single-Expression

Java

public double cube(double x) {
  return x * x * x;
}

Kotlin

fun cube(x: Double) : Double = x * x * x

Vararg

Java

public int sum(int... numbers) { }

Kotlin

fun sum(vararg x: Int) { }

Main

Java

public class MyClass {
    public static void main(String[] args) {  }
}

Kotlin

fun main(args: Array<String>) {  }

Named Arguments

Java

public static void main(String[]args){
  openFile("file.txt", true);
}

public static File openFile(String filename, boolean readOnly) { }

Kotlin

fun main(args: Array<String>) {
  openFile("file.txt", readOnly = true)
}

fun openFile(filename: String, readOnly: Boolean) : File { }

Optional Arguments

Java

public static void main(String[]args){
  createFile("file.txt");

  createFile("file.txt", true);

  createFile("file.txt", true, false);

  createExecutableFile("file.txt");
}

public static File createFile(String filename) { }

public static File createFile(String filename, boolean appendDate) { }

public static File createFile(String filename, boolean appendDate, boolean executable) { }

public static File createExecutableFile(String filename) { }

Kotlin

fun main(args: Array<String>) {
  createFile("file.txt")

  createFile("file.txt", true)
  createFile("file.txt", appendDate = true)

  createFile("file.txt", true, false)
  createFile("file.txt", appendDate = true, executable = true)

  createFile("file.txt", executable = true)
}

fun createFile(filename: String, appendDate: Boolean = false, executable: Boolean = false): File { }

Generic Methods

Java

public void init() {
  List<String> moduleInferred = createList("net");
}

public <T> List<T> createList(T item) { }

Kotlin

fun init() {
  val module = createList<String>("net")
  val moduleInferred = createList("net")
}

fun <T> createList(item: T): List<T> { }

Data Classes - Destructuring

Java

public static void main(String[]args) {
    Book book = createBook();

    System.out.println(book);
    System.out.println("Title: " + book.title);
}

public static Book createBook(){
    return new Book("title_01", "author_01");
}

public class Book {
    final private String title;
    final private String author;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    @Override
    public String toString() {
        return "Title: " + title + " Author: " + author;
    }
}

Kotlin

fun main(args: Array<String>) {
    val book = createBook();
    // or
    val (title, author) = createBook()

    println(book)
    println("Title: $title")
}

fun createBook() : Book{
    return Book("title_01", "author_01")
}

data class Book(val title: String, val author: String)

愿大家都能飞得更高、飞得更开心…嗯,开心很重要…


欢迎来撩

frendy

It will shine for us...