Android kotlin 与java的对比方法大全2

1.基本函数:
Java

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

Kotlin

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

2.传参数函数:
Java

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

Kotlin

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

3.传参数带默认值函数:
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!")
}

4.带返回值的函数:
Java

public boolean hasItems() {
  return true;
}

Kotlin

fun hasItems() : Boolean {
    return true
}

5.单表达式函数:
Java

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

Kotlin

fun cube(x: Double) : Double = x * x * x
  1. 可变参数数量(vararg ):
    Java
public int sum(int... numbers) { }

Kotlin

fun sum(vararg x: Int) { }

7.Main函数:
Java

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

    }
}

Kotlin

fun main(args: Array<String>) {

}

8.参数命名:
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 { }

9.方法重载(可选参数):
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 { }

10.泛型函数:
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> { }

11.数据类:
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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容