Scala学习笔记(二)Case的作用
来源:互联网 发布:python解压zip文件 编辑:程序博客网 时间:2024/05/17 04:24
Case classes and case objects are defined like a normal classes or objects, except that the definitions is prefixed with the modifier case.
1. Case classes implicitly come with a constructor function, with the same name as the class.
2. Case classes and case objects implicitly come with implementations of methods toString, equals and hashCode, which override the methods with the same name in class AnyRef.
3. Case classes implicitly come with nullary accessor methods which retrieve the constructor agruments
4. Case classes allow the constructions of patterns which refer to the case class constructor
其实翻译过来按照笔者的理解Case Class只是在Class的声明前面添加了Case这个标记符,从而让编译器识别出来然后帮助完成一些类似JavaBean风格的冗余代码。比如依照Class的声明添加了合适的构造函数,重写了toString、equals等方法,并在生成Java代码的时候,优雅的生成了符合JavaBean规范的getter/setter。另外在使用这样的Class构造实例的时候,可以利用pattern matching省去new这样的关键字。
举个简单的例子:(其实getter和setter的自动添加还可以通过@BeanProperty这个Annotation来实现)
case class Person(firstName: String, lastName: String) val me = Person("Daniel", "Jin")val firstN = me.firstNameval lastN = me.lastName println(me == Person(first, last))println(me)
- Scala学习笔记(二)Case的作用
- SCALA学习笔记(二)
- scala学习笔记(二)
- scala学习笔记(二)
- scala 学习笔记(二)
- Scala学习笔记(二)
- scala学习笔记(二)
- case关键字在scala的作用
- scala学习笔记08--Option和case
- scala学习笔记08--Option和case
- Scala学习笔记二
- scala学习笔记二
- Scala学习笔记 --- scala中 => 作用
- Scala学习笔记(二)Scala面向对象编程
- scala学习笔记(二) 控制语句
- Scala学习笔记(二) ---- 基础简述
- Scala学习笔记(二)流程控制
- [Scala]Scala学习笔记二 数组
- Orcal sql处理字符串
- TKG3d--Package Geom
- Eclipse快捷键指南
- Box-Shadow常被遗忘的一个参数——“Spread”
- windbg调式命令备忘录
- Scala学习笔记(二)Case的作用
- 理解prototype
- XMemcachedClient实例的add方法和set方法区别
- Struts 体系结构与工作原理(图) .
- DataGridView控件相关使用总结
- 子程序设计——用栈传递参数
- CSS3 制作Drop Shadow效果
- android UI进阶之style和theme的使用
- TIME_WAIT状态的作用