码迷,mamicode.com
首页 > 其他好文 > 详细

Scala 面向对象编程之类

时间:2019-08-29 23:27:34      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:val   代码块   class   自己   内部类   col   lib   struct   辅助   

定义一个简单的类

// 定义类,包含field以及方法

class HelloWorld {

  private var name = "leo"

  def sayHello() { print("Hello, " + name) }  

  def getName = name

}

 

// 创建类的对象,并调用其方法

val helloWorld = new HelloWorld

helloWorld.sayHello()

print(helloWorld.getName) // 也可以不加括号,如果定义方法时不带括号,则调用方法时也不能带括号

 

 

gettersetter

 

// 定义不带privatevar field,此时scala生成的面向JVM的类时,会定义为privatename字段,并提供publicgettersetter方法

 

// 而如果使用private修饰field,则生成的gettersetter也是private

 

// 如果定义val field,则只会生成getter方法

 

// 如果不希望生成settergetter方法,则将field声明为private[this]

 

class Student {

 

  var name = "leo"

 

}

 

 

 

// 调用gettersetter方法,分别叫做namename_ =

 

val leo = new Student

 

print(leo.name)

 

leo.name = "leo1"

 

自定义gettersetter

// 如果你不希望fieldsetter方法,则可以定义为val,但是此时就再也不能更改field的值了

// 但是如果希望能够仅仅暴露出一个getter方法,并且还能通过某些方法更改field的值,那么需要综合使用private以及自定义getter方法

// 此时,由于fieldprivate的,所以settergetter都是private,对外界没有暴露;自己可以实现修改field值的方法;自己可以覆盖getter方法

class Student {

  private var myName = "leo"

 

  def updateName(newName: String) {

    if(newName == "leo1") myName = newName

    else print("not accept this new name!!!")

  }

 

  def name = "your name is " + myName

}

 

仅暴露fieldgetter方法

// 如果你不希望fieldsetter方法,则可以定义为val,但是此时就再也不能更改field的值了

// 但是如果希望能够仅仅暴露出一个getter方法,并且还能通过某些方法更改field的值,那么需要综合使用private以及自定义getter方法

// 此时,由于fieldprivate的,所以settergetter都是private,对外界没有暴露;自己可以实现修改field值的方法;自己可以覆盖getter方法

class Student {

  private var myName = "leo"

 

  def updateName(newName: String) {

    if(newName == "leo1") myName = newName

    else print("not accept this new name!!!")

  }

 

  def name = "your name is " + myName

}

 

仅暴露fieldgetter方法

// 如果你不希望fieldsetter方法,则可以定义为val,但是此时就再也不能更改field的值了

// 但是如果希望能够仅仅暴露出一个getter方法,并且还能通过某些方法更改field的值,那么需要综合使用private以及自定义getter方法

// 此时,由于fieldprivate的,所以settergetter都是private,对外界没有暴露;自己可以实现修改field值的方法;自己可以覆盖getter方法

class Student {

  private var myName = "leo"

 

  def updateName(newName: String) {

    if(newName == "leo1") myName = newName

    else print("not accept this new name!!!")

  }

 

  def name = "your name is " + myName

}

 

Java风格的gettersetter方法

 

// Scalagettersetter方法的命名与java是不同的,是fieldfield_=的方式

// 如果要让scala自动生成java风格的gettersetter方法,只要给field添加@BeanProperty注解即可

// 此时会生成4个方法,name: Stringname_=(newValue: String): UnitgetName(): StringsetName(newValue: String): Unit

import scala.reflect.BeanProperty

class Student {

  @BeanProperty var name: String = _

}

class Student(@BeanProperty var name: String)

 

val s = new Student

s.setName("leo")

s.getName()

 

辅助constructotr

// Scala中,可以给类定义多个辅助constructor,类似于java中的构造函数重载

// 辅助constructor之间可以互相调用,而且必须第一行调用主constructor

class Student {

  private var name = ""

  private var age = 0

  def this(name: String) {

    this()

    this.name = name

  }

  def this(name: String, age: Int) {

    this(name)

    this.age = age

  }

}

 

constructor

// Scala中,主constructor是与类名放在一起的,与java不同

// 而且类中,没有定义在任何方法或者是代码块之中的代码,就是主constructor的代码,这点感觉没有java那么清晰

class Student(val name: String, val age: Int) {

  println("your name is " + name + ", your age is " + age)

}

 

// constructor中还可以通过使用默认参数,来给参数默认的值

class Student(val name: String = "leo", val age: Int = 30) {

  println("your name is " + name + ", your age is " + age)

}

 

// 如果主constrcutor传入的参数什么修饰都没有,比如name: String,那么如果类内部的方法使用到了,则会声明为private[this] name;否则没有该field,就只能被constructor代码使用而已

 

内部类

 

// Scala中,同样可以在类中定义内部类;但是与java不同的是,每个外部类的对象的内部类,都是不同的类

import scala.collection.mutable.ArrayBuffer

class Class {

  class Student(val name: String) {}

  val students = new ArrayBuffer[Student]

  def getStudent(name: String) =  {

    new Student(name)

  }

}

 

val c1 = new Class

val s1 = c1.getStudent("leo")

c1.students += s1

 

val c2 = new Class

val s2 = c2.getStudent("leo")

c1.students += s2

 

Scala 面向对象编程之类

标签:val   代码块   class   自己   内部类   col   lib   struct   辅助   

原文地址:https://www.cnblogs.com/YuanWeiBlogger/p/11432394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!