kotlin与java的比较:数据类
Posted 彬sir哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kotlin与java的比较:数据类相关的知识,希望对你有一定的参考价值。
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)
以上是关于kotlin与java的比较:数据类的主要内容,如果未能解决你的问题,请参考以下文章