Chapter 5 -- Scala for the Impatient

本文深入探讨了Scala中类的定义与使用,包括默认公开的类定义、辅助构造器的实现方式及其调用过程、主构造器的特性,以及内部类的应用。同时介绍了Scala与Java在字段访问器(getter和setter)上的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##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 constructor
val p2 = new Person("name")  // call auxiliary constructor
val 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.ArrayBuffer
class 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 Network
val myFace = new Network

val fred = chatter.join("Fred")  // the type of fred is chatter.Member
val wilma = chatter.join("wilma") 
fred.contacts += wilma
val barney = myFace.join("Barney") // the type of barney  is myFace.Member
fred.contacts += barney // invalid cause the type of fred and barney is differents                            

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值