您的位置:首页 > 其它

Scala 深入浅出实战经典 第7讲:Scala类的属性和对象私有字段实战详解

2015-08-14 01:42 399 查看
一、课程前导

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

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

百度云盘:http://pan.baidu.com/s/1c0noOt6

腾讯微云:http://url.cn/TnGbdC

360云盘:http://yunpan.cn/cQ4c2UALDjSKy  访问密码 45e2

Scala是最为重要的大数据语言,该视频每天还在持续跟新中,预计大约有140讲,请您根据上述视频彻底掌握Scala实战技术。

《第7讲:Scala类的属性和对象私有字段实战详解》的视频地址:

酷6:http://v.ku6.com/show/_bvsoz8t5fseMzJ-i4saNg...html

51cto:http://edu.51cto.com/lesson/id-66506.html

最后,王家林老师个人的微信是18610086859

二、课程内容

注:下面的笔记源于王家林老师的第7讲:Scala类的属性和对象私有字段实战详解、Lazy的使用视频和《快速Scala》书。

第7讲:Scala类的属性和对象私有字段实战详解

1、Scala类的使用实战

2、getter与setter实战

3、对象私有属性实战

一、Scala类的实战

//没有Public,默认是Public级别Class Person{Private var age = 0 //var可改变值的,private级别,与java不同的是这个age必须赋值Def increment(){age += 1} Def current = age}Class Student{ Var age= 0 //声明一属性,默认private,与java不同,默认生成age的getter,setter,在scala对应为age的age,age_}Object HelloOOP{Def
main(args:Array[String]:Unit = {Val person = new Person()Person.increment()Println(person.current)}

二、getter与setter实战

Val student = new StudentStudent.age = 10 //调用Student类自动生成的Student.setAge,在Scala,是age_ ,来赋值Println(Student.age) //通过Student类自动生成的Student.getAge,在Scala,是age,来取值打印Class Student{ private var privateAge = 0 val name = “Scala” //自动生成final和getter,也就是只读属性

def age = privateAge}Object HelloOOP{Def main(args:Array[String]:Unit = {Val student = new Student//Student.name = 10 报错,只能读不能写Println(Student.name)}三、对象私有属性实战

Class Student{ private[this] var privateAge = 0 //类的方法只能访问自己对象的私有属性,不能访问其他对象的属性,也就是当前对象私有 val name = “Scala” //自动生成final和getter,也就是只读属性

def age = privateAge//方法可以访问这个类的所有私有字段 def isYonger(other : Student) = privateAge < other.privateAge //若privateAge设为对象私有,则报错,因为other是Student派生的对象,那么other中也有这个字段,属于other对象私有,不能被别的对象访问}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息