Kotlin-函数

本文详细比较了Java和Kotlin中的函数定义、参数传递、默认参数、返回值、单一表达式、可变参数、主函数以及泛型和数据类型的使用。展示了两种语言在这些方面的不同之处。
摘要由CSDN通过智能技术生成

基本函数

Java

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

Kotlin

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

函数参数

Java

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

Kotlin

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

函数默认参数

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!")
}

函数返回结果

Java

public boolean hasItems() {
  return true;
}

Kotlin

fun hasItems() : Boolean {
    return true
}

单一表达式函数

Java

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

Kotlin

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

可变长度函数

Java

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

Kotlin

fun sum(vararg x: Int) { }

主函数

Java

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

    }
}

Kotlin

fun main(args: Array<String>) {

}

指定参数函数

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 { }

可选参数函数

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 { }

泛型函数

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> { }

数据类型函数

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值