Kotlin数据类

在本教程中,我们将学习Kotlin的数据类。如果您还没有阅读过Kotlin类的文章,我们建议您在继续之前先阅读一下。

Kotlin的数据类

Kotlin Data Class
public class Book {

    private String name;
    private String authorName;
    private long lastModifiedTimeStamp;
    private float rating;
    private int downloads;


    public Book(String name, String authorName, long lastModified, float rating, int downloads) {
        this.name = name;
        this.authorName = authorName;
        this.lastModifiedTimeStamp = lastModified;
        this.rating = rating;
        this.downloads = downloads;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public long getLastModifiedTimeStamp() {
        return lastModifiedTimeStamp;
    }

    public void setLastModifiedTimeStamp(long lastModifiedTimeStamp) {
        this.lastModifiedTimeStamp = lastModifiedTimeStamp;
    }

    public float getRating() {
        return rating;
    }

    public void setRating(float rating) {
        this.rating = rating;
    }

    public int getDownloads() {
        return downloads;
    }

    public void setDownloads(int downloads) {
        this.downloads = downloads;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book that = (Book) o;

        if (downloads != that.downloads)
            return false;
        if (name != null ? !name.equals(that.name) :
                that.name != null) {
            return false;
        }
        return authorName != null ?
                authorName.equals(that.authorName) :
                that.authorName == null;

    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (authorName != null ?
                authorName.hashCode() : 0);
        result = 31 * result + downloads;
        return result;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + authorName + '\'' +
                ", lastModifiedTimestamp='" + lastModifiedTimeStamp + '\'' +
                ", rating='" + rating + '\'' +
                ", downloads=" + downloads +
                '}';
    }
}

哇!仅仅用于存储5个字段的对象就需要96行代码。除了getter和setter、toString()、equals()和hashCode()方法之外,我们在这里没有做太多事情。在我们的实践中,有了清晰的架构和代码分离的实践,我们需要创建POJO类,因为每个项目都需要存储数据。这可能会增加样板代码的量。这就是Kotlin出场的地方,它使用了数据类来减少样板代码。上述的POJO类可以用以下方式在Kotlin中编写:

data class Book(var name: String, var authorName: String, var lastModified: Long, var rating: Float, var downloads: Int)

就是这样。Kotlin把96行Java代码转化为一行代码。这是Kotlin减少项目中样板代码的方式!

创建Kotlin数据类

创建Kotlin Data类的要求如下:

  • You need to append the class with the keyword data
  • The primary constructor needs to have at least one parameter.
  • Each parameter of the primary constructor must have a val or a var assigned. This isn’t the case with a normal class, where specifying a val or a var isn’t compulsory.
  • Data classes cannot be appended with abstract, open, sealed or inner

Kotlin数据类内置方法

Kotlin 数据类会自动为你创建以下函数。

  • equals() and hashCode()
  • toString() of the form “Book(name=JournalDev, authorName=Anupam)”
  • componentN() functions for each of the parameters in the order specified. This is known as destructuring declarations.
  • copy()

Kotlin数据类的特性

以下是数据类提供的一些特性。

  • To create a parameterless constructor, specify default values to each of the parameters present in the primary constructor.
  • A Data Class allows subclassing(No need to mention the keyword open).
  • You can provide explicit implementations for the functions equals() hashCode() and toString()
  • Explicit implementations for copy() and componentN() functions are not allowed.
  • We can control the visibility of the getters and setters by specifying the visibility modifiers in the constructor as shown below.
    data class Book(var name: String,private var authorName: String, var lastModified: Long, var rating: Float, var downloads: Int)

  • A val parameter won’t have a setter defined implicitly(can’t be done explicitly too!).

数据类中的默认参数和命名参数

以下是我们的数据类:

data class Book(var name: String, var authorName: String, var lastModified: Long, var rating: Float, var downloads: Int)

所有参数都没有设置默认值。因此,我们需要在实例化时为每个参数设置一个参数,如下所示。

fun main(args: Array<String>) {
val book = Book("Android Tutorials","Anupam", 1234567, 4.5f, 1000)
}

让我们设置一些默认参数,看看实例化的变化。


data class Book(var name: String, var authorName: String = "Anupam", var lastModified: Long = 1234567, var rating: Float = 5f, var downloads: Int = 1000)
fun main(args: Array<String>) {
var book = Book("Android tutorials","Anupam", 1234567, 4.5f, 1000)

    book = Book("Kotlin")
    book = Book("Swift",downloads = 500)
    book = Book("Java","Pankaj",rating = 5f, downloads = 1000)
    book = Book("Python","Shubham",rating = 5f)

}

与其设置每个参数,我们可以只设置非默认参数和希望设置的参数,使用命名参数。通过使用命名参数,我们可以通过明确指定参数名后跟 = 来将第5个参数设为第二个参数。以这种方式生活会更轻松!

Kotlin 数据类的 toString() 方法

toString()方法是隐式创建的,并打印出实例的参数名称和标签,如下所示。

data class Book(var name: String, var authorName: String = "Anupam", var lastModified: Long = 1234567, var rating: Float = 5f, var downloads: Int = 1000)


fun main(args: Array<String>) {

    var book = Book("Android tutorials","Anupam", 1234567, 4.5f, 1000)
    println(book)
    book = Book("Kotlin")
    println(book)
    book = Book("Swift",downloads = 500)
    println(book)
    book = Book("Java","Pankaj",rating = 5f, downloads = 1000)
    println(book.toString())
    book = Book("Python","Shubham",rating = 5f)
    println(book.toString())


}

//Following is printed in the console.
//Book(name=Android tutorials, authorName=Anupam, lastModified=1234567, rating=4.5, downloads=1000)
//Book(name=Kotlin, authorName=Anupam, lastModified=1234567, rating=5.0, downloads=1000)
//Book(name=Swift, authorName=Anupam, lastModified=1234567, rating=5.0, downloads=500)
//Book(name=Java, authorName=Pankaj, lastModified=1234567, rating=5.0, downloads=1000)
//Book(name=Python, authorName=Shubham, lastModified=1234567, rating=5.0, downloads=1000)

注意:打印函数会隐式地调用toString()方法。

Kotlin数据类的copy()方法的释义是

使用复制函数可以创建数据类实例的副本,其中某些属性被修改。建议在数据类的构造函数中使用val参数,以便使用实例的不可变属性。在处理多线程应用程序时,不可变对象更容易处理。因此,通过只更改少量属性来创建不可变对象的副本,copy()函数非常方便。

data class Book(val name: String, val authorName: String = "Anupam", val lastModified: Long = 1234567, val rating: Float = 5f, val downloads: Int = 1000)

fun main(args: Array<String>) {

    val book = Book("Android tutorials","Anupam", 1234567, 4.5f, 1000)
    println(book)

    val newBook = book.copy(name = "Kotlin")
    println(newBook)
}
//Following is printed in the console.
//Book(name=Android tutorials, authorName=Anupam, lastModified=1234567, rating=4.5, downloads=1000)
//Book(name=Kotlin, authorName=Anupam, lastModified=1234567, rating=4.5, downloads=1000)

Kotlin数据类的equals()和hashCode()方法

hashCode() 方法返回对象的哈希码。如果两个对象相等,hashCode() 方法会产生相同的整数结果。因此,equals() 方法会在 hashCode() 相等时返回 true,否则返回 false。

data class Book(val name: String, val authorName: String = "Anupam", val lastModified: Long = 1234567, val rating: Float = 5f, val downloads: Int = 1000)

fun main(args: Array<String>) {

    val book = Book("Android tutorials","Anupam", 1234567, 4.5f, 1000)
    println("Hashcode is ${book.hashCode()}")

    val newBook = book.copy(name = "Kotlin")
    println("Hashcode is ${newBook.hashCode()}")

    val copyBook = book.copy()
    println("Hashcode is ${copyBook.hashCode()}")


    if(copyBook.equals(book))
        println("copyBook and book are equal")

    if(!book.equals(newBook))
        println("newBook and book are NOT equal")

}

//Following is printed in the console.
//Hashcode is 649213087
//Hashcode is 1237165820
//Hashcode is 649213087
//copyBook and book are equal
//newBook and book are NOT equal

第一个和第三个对象的哈希码相同,因此它们是相等的。注意:在 Kotlin 中,equals() 方法等同于 ==。

解构声明

kotlin data classes component functions
data class Book(val name: String, val authorName: String = "Anupam", val lastModified: Long = 1234567, val rating: Float = 5f, val downloads: Int = 1000)

fun main(args: Array<String>) {

    val book = Book("Android tutorials","Anupam", 1234567, 4.5f, 1000)

    println(book.component1()) //Android tutorials
    println(book.component2()) //Anupam
    println(book.component3()) //1234567
    println(book.component4()) //4.5
    println(book.component5()) //1000
    
}

解构声明允许我们从类对象中以属性的方式访问参数,如下所示。

data class Book(val name: String, val authorName: String = "Anupam", val lastModified: Long = 1234567, val rating: Float = 5f, val downloads: Int = 1000)

fun main(args: Array<String>) {

    val book = Book("Android tutorials","Anupam", 1234567, 4.5f, 1000)
    val (n,a,date,rating,downloads) = book
}

注意:如果在任何参数上设置了像private这样的可见性修饰符,在上述函数中将无法访问。

data class Book(val name: String,private val authorName: String = "Anupam", val lastModified: Long = 1234567, val rating: Float = 5f, val downloads: Int = 1000)

fun main(args: Array<String>) {

    val book = Book("Android tutorials","Anupam", 1234567, 4.5f, 1000)
    val (n,a,date,rating,downloads) = book //This won't compile since authorName is private
}

对于Kotlin数据类的快速概述就是这些。参考资料:Kotlin文档。

发表回复 0

Your email address will not be published. Required fields are marked *


广告
将在 10 秒后关闭
bannerAds