博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Chapter 5 -- Scala for the Impatient
阅读量:6715 次
发布时间:2019-06-25

本文共 2608 字,大约阅读时间需要 8 分钟。

hot3.png

##define class // default public class Counter {

private var value = 0    def increment() {value += 1}    def current() = value}

##using class val myCounter = new Counter myCounter.increment() println(myCounter.current) // you can ignore the bracket if the method of instance is not side effect

##getter and setter which is generated to field val/var name public name/ name_= ( for var)

@BeanProperty val/var name public name / getName() / name_= (for var) / setName(..) (for var)

private val/name private name / name_= (for var)

private[this] val/var name -

private[class name] val/var name dependents on implementation

auxiliary constructor

class Person {    private var name = ""    private age = 0    def this(name: String){        this() // call primary constructor        this.name = name    }    def this(name: String, age: Int) { // another auxiliary constructor        this(name)        this.age = age    }}val p1 = new Person  // call primary constructorval p2 = new Person("name")  // call auxiliary constructorval p3 = new Person("Fred", 32) // call auxiliary constructor

primary constructor

class Person(val name: String, val age: Int){    //primary contructor will execute all code that are included in this scope}//above equals java code:public class Person{    private String name;    private int age;    public Person(String name, int age){        this.name = name;        this.age = age;    }    //getter...    //setter...}

##getter and setter which is generated to primary constructor scala's getter is "name" and setter is "name_=(...)"

java's getter is "getName()" and setter is "setName(..)"

name: String private object field. It'll be not exists if it isn't used.

private val/var name: String private field, private setter and getter method

val/var name: String private field, public setter and getter method

@BeanProperty val/var name: String private field, public scala/java setter and getter method

inner class

import scala.collection.mutable.ArrayBufferclass Network {    class Member(val name: String){        val contacts = new ArrayBuffer[Member]    }    private val members = new ArrayBuffer[Member]    def join(name: String) = {        val m = new Member(name)        members += m        m    }    }val chatter = new Networkval myFace = new Networkval fred = chatter.join("Fred")  // the type of fred is chatter.Memberval wilma = chatter.join("wilma") fred.contacts += wilmaval barney = myFace.join("Barney") // the type of barney  is myFace.Memberfred.contacts += barney // invalid cause the type of fred and barney is differents

转载于:https://my.oschina.net/zjzhai/blog/379573

你可能感兴趣的文章
How Many Tables HDOJ
查看>>
DataTable转换成List
查看>>
身份证号码验证算法
查看>>
py实现ftp
查看>>
3、异步编程-JS种事件队列的优先级
查看>>
关于C语言判断文件尾问题的探讨
查看>>
poj1243(经典dp)
查看>>
svn仓库转为git仓库
查看>>
跳转到指定的控制器
查看>>
cocoapod升级版本
查看>>
在正式800修改代码
查看>>
AngularJs的UI组件ui-Bootstrap分享(十三)——Progressbar
查看>>
用前序遍历递归构造二叉树
查看>>
JavaScript jQuery bootstrap css ajax
查看>>
组合选择器
查看>>
Understanding Angular’s $apply() and $digest()
查看>>
HTML之列表
查看>>
Global.asax文件说明
查看>>
(十六)SpringBoot之使用 Caching- - EhCache
查看>>
ubuntu制作apt源
查看>>