- Scala Programming Projects
- Mikael Valot Nicolas Jorand
- 178字
- 2021-07-23 16:25:15
Case class
In Scala, we define most data structures using case classes. case class has one to many immutable attributes and provides several built-in functions compared to a standard class.
Type the following into the worksheet:
case class Person(name: String, age: Int)
val mikaelNew = new Person("Mikael", 41)
// 'new' is optional
val mikael = Person("Mikael", 41)
// == compares values, not references
mikael == mikaelNew
// == is exactly the same as .equals
mikael.equals(mikaelNew)
val name = mikael.name
// a case class is immutable. The line below does not compile:
//mikael.name = "Nicolas"
// you need to create a new instance using copy
val nicolas = mikael.copy(name = "Nicolas")
In the preceding code, the text following // is a comment that explains the preceding statement.
When you declare a class as case class, the Scala compiler automatically generates a default constructor, an equals and hashCode method, a copy constructor, and an accessor for each attribute.
Here is a screenshot of the worksheet we have. You can see the results of the evaluations on the right-hand side:
推薦閱讀
- RCNP實驗指南:構建高級的路由互聯網絡(BARI)
- Truffle Quick Start Guide
- Hands-On Full Stack Development with Spring Boot 2 and React(Second Edition)
- 局域網組建、管理與維護項目教程(Windows Server 2003)
- 網絡的琴弦:玩轉IP看監(jiān)控
- 物聯網時代
- 面向物聯網的嵌入式系統開發(fā):基于CC2530和STM32微處理器
- NB-IoT物聯網技術解析與案例詳解
- Metasploit Penetration Testing Cookbook
- CCNP TSHOOT(642-832)認證考試指南
- Master Apache JMeter:From Load Testing to DevOps
- 互聯網+思維與創(chuàng)新:通往未來的+號
- Getting Started with Memcached
- Practical Web Penetration Testing
- 網絡空間作戰(zhàn):機理與籌劃