Java 14:记录
Java 14是在幾周前問世的,它引入了Record類型,它是一個(gè)不變的數(shù)據(jù)載體類,旨在容納一組固定的字段。 請(qǐng)注意,這是預(yù)覽語言功能 ,這意味著必須使用--enable-preview標(biāo)志在Java編譯器和運(yùn)行時(shí)中顯式啟用它。
我將直接介紹一個(gè)Book記錄示例,該記錄旨在保存書名,作者,出版日期和價(jià)格。 這是記錄類的聲明方式:
public record Book(String title, String author, LocalDate publishDate, double price) { }您可以使用javap查看編譯器自動(dòng)生成的代碼:
public final class Book extends java.lang.Record { public Book(java.lang.String, java.lang.String, java.time.LocalDate, double ); public java.lang.String title(); public java.lang.String author(); public java.time.LocalDate publishDate(); public double price(); public java.lang.String toString(); public final int hashCode(); public final boolean equals(java.lang.Object); }如上所示,編譯器自動(dòng)生成了構(gòu)造函數(shù),getter方法, hashCode , equals和toString ,從而使我們不必鍵入很多樣板代碼。
但是,記錄不僅可以節(jié)省鍵入時(shí)間。 它們還使您的意圖明確了,您希望將不可變數(shù)據(jù)項(xiàng)建模為一組相關(guān)字段。
用于現(xiàn)場驗(yàn)證的緊湊型構(gòu)造器
現(xiàn)在,假設(shè)您要向記錄添加驗(yàn)證和默認(rèn)值。 例如,您可能要驗(yàn)證未以負(fù)價(jià)或未來發(fā)布日期創(chuàng)建Book記錄。 可以使用緊湊的構(gòu)造函數(shù)來完成此操作,如下所示:
public record Book(String title, String author, LocalDate publishDate, double price) { //compact constructor (no parameter list), used for validation and setting defaults public Book { if (price < 0.0 ) { throw new IllegalArgumentException( "price must be positive" ); } if (publishDate != null && publishDate.isAfter(LocalDate.now())) { throw new IllegalArgumentException( "publishDate cannot be in the future" ); } this .author = author == null ? "Unknown" : author; } }緊湊的構(gòu)造函數(shù)沒有參數(shù)列表。 它驗(yàn)證價(jià)格和發(fā)布日期,并為作者設(shè)置默認(rèn)值。 在此構(gòu)造函數(shù)中未分配的字段(即title , publishDate和price )在此構(gòu)造函數(shù)的末尾隱式初始化。
替代構(gòu)造函數(shù)和其他方法
記錄使您可以定義其他方法,構(gòu)造函數(shù)和靜態(tài)字段,如下面的代碼所示。 但是,請(qǐng)記住,從語義上說,一條記錄被設(shè)計(jì)為數(shù)據(jù)載體,因此,如果您覺得要添加額外的方法,則可能是需要一個(gè)類而不是一條記錄。
public record Book(String title, String author, LocalDate publishDate, double price) { // static field private static final String UNKNOWN_AUTHOR = "UNKNOWN" ; // compact constructor, used for validation and setting defaults public Book { if (price < 0 ) { throw new IllegalArgumentException( "price must be positive" ); } if (publishDate != null && publishDate.isAfter(LocalDate.now())) { throw new IllegalArgumentException( "publishDate cannot be in the future" ); } this .author = author == null ? UNKNOWN_AUTHOR : author; ? UNKNOWN_AUTHOR : author; } // static factory constructor public static Book freeBook(String title, String author, LocalDate publishDate) { return new Book(title, author, publishDate, 0.0 ); } // alternative constructor, without an author public Book(String title, LocalDate publishDate, double price) { this (title, null , publishDate, price); } // additional method to get the year of publish public int publishYear() { return publishDate.getYear(); } // override toString to make it more user friendly @Override public String toString() { return String.format( "%s (%tY) by %s for £%.2f" , title, publishDate, author, price); } }翻譯自: https://www.javacodegeeks.com/2020/04/java-14-records.html
總結(jié)
以上是生活随笔為你收集整理的Java 14:记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hazelcast_Hazelcast的
- 下一篇: java与java ee_计划Java