Case class和Case object代码实战解析之Scala学习笔记-20

package com.leegh.pattern_match

/**
* @author Guohui Li
*/

abstract class Person
case class Student(age: Int) extends Person
case class Worker(age: Int, salary: Double) extends Person
case object Shared extends Person

object case_class_object {

def main(args: Array[String]): Unit = {

def caseOps(person: Person) = person match {
case Student(age) => println("I am " + age + "years od")
case Worker(_, salary) => println("Wow,I got " + salary)
case Shared => println("No property")
}
caseOps(Student(19))
caseOps(Shared)

val worker = Worker(29, 10000.1)
val worker2 = worker.copy(salary = 19.95)
val worker3 = worker.copy(age = 30)

}
}

附:

本博客说明:

1.整理思路,提高自己。

2.受教于王家林老师,?有所收获,故推荐。

3.博客注重实践,多余的文字就不多说了,都是做技术的。

4.信息来源于 DT大数据梦工厂微信公众账号:DT_Spark。?

DT大数据梦工厂的微信公众号是DT_Spark,每天都会有大数据实战视频发布,请您持续学习。

Scala 深入浅出实战经典(1-64讲)完整视频、PPT、代码下载:

百度云盘:http://pan.baidu.com/s/1c0noOt6
腾讯微云:http://url.cn/TnGbdC
360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2

时间: 2024-10-12 22:26:58

Case class和Case object代码实战解析之Scala学习笔记-20的相关文章

Type、Array、List、Tuple模式匹配实战解析之Scala学习笔记-18

package com.leegh.pattern_match /** * @author Guohui Li *//** * Type,Array模式匹配 */object Pattern_Match_More { def main(args: Array[String]): Unit = { def match_type(t: Any) = t match { case p: Int => println("It is Integer") case p: String =&g

Scala深入浅出实战经典:29,Case class和Case object代码实战解析

今天学习了王家林老师scala讲座的第29讲,case class和case object的应用实战.做下记录. 信息来源于 DT大数据梦工厂微信公众账号:DT_Spark 关注微信账号,获取更多关于王家林老师的课程内容 王老师QQ:1740415547 微信号:18610086859 话不多说,让我们从代码出发: abstract class People//定义抽象类case class Student(age:Int) extends People//定义case class,继承抽象类c

Scala类型约束代码实战及其在Spark中的应用源码解析之Scala学习笔记-39

package com.leegh.parameterization /** * @author Guohui Li */object Type_Contraints { def main(args: Array[String]): Unit = { def rocky[T](i: T)(implicit ev: T <:< java.io.Serializable) { println("Life is too short,you need Spark!") } rock

Scala中Context Bounds代码实战及其在Spark中的应用源码解析之Scala学习笔记-36

package com.leegh.parameterization /** * @author Guohui Li */ class Pair_Ordering[T: Ordering](val first: T, val second: T) { def bigger(implicit ordered: Ordering[T]) = { if (ordered.compare(first, second) > 0) first else second }} object Context_Bo

Scala中类型变量Bounds代码实战及其在Spark中的应用源码解析之Scala学习笔记-34

package com.leegh.parameterization /** * @author Guohui Li */class Pair[T <: Comparable[T]](val first: T, val second: T) { def bigger = if (first.compareTo(second) > 0) first else second} class Pair_Lower_Bound[T](val first: T, val second: T) { def

Scala中Variance代码实战及其在Spark中的应用源码解析之Scala学习笔记-40

package com.leegh.parameterization /** * @author Guohui Li */ class Personclass Student extends Personclass C[+T](val args: T) trait Friend[-T] { def makeFriend(somebody: T)}object Variance { def makeFriendWithYou(s: Student, f: Friend[Student]) { f.

ClassTag 、Manifest、ClassManifest、TypeTag代码实战及其在Spark中的应用源码解析之Scala学习笔记-37

package com.leegh.parameterization import scala.reflect.ClassTag /** * @author Guohui Li */class A[T] object Manifest_ClassTag { def main(args: Array[String]): Unit = { def arrayMake[T: Manifest](first: T, second: T) = { val r = new Array[T](2); r(0)

Scala中View Bounds代码实战及其在Spark中的应用源码解析之Scala学习笔记-35

package com.leegh.parameterization /** * @author Guohui Li */class Pair_NotPerfect[T <% Comparable[T]](val first: T, val second: T) { def bigger = if (first.compareTo(second) > 0) first else second} //class Pair_NotPerfect[T <: Comparable[T]](val

Scala并发编程实战初体验及其在Spark源码中的应用解析之Scala学习笔记-56

package com.leegh.actor import scala.actors.Actor /** * @author Guohui Li */object First_Actor extends Actor { def act() { for (i <- 1 to 10) { println("Step : " + i) println(Thread.currentThread().getName) Thread.sleep(2000) } }} object Seco