欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Scala入门三——面向对象

程序员文章站 2022-06-14 22:08:39
...

1. 类的声明和private关键字说明

package com.ghq.scala.oo


/**
 * private[ghq]:限定当前类的访问包路径为ghq,及其子包
 * 
 * Person 后面跟的private限定该类的构造方法为私有,只有该类的伴生对象可以使用构造方法
 * 
 * Person 后面跟的private[this] 限定该类的构造方法为私有,该类的伴生对象也不可以使用构造方法
 */
private[ghq] class Person private {

  val id = 23
  var name = "zhangsan"

  //private:限定该属性只能由本类或者其伴生对象访问
  private var gender = "male"

  //private[this]:限定该属性只能由本类访问
  private[this] var addr:String = _

  def getAddr():String={
    addr
  }

}

/**
 * object Person 称之为 class Person的伴生对象
 */
object Person{
  def main(args: Array[String]): Unit = {
    val p = new Person
    println(p.id)
    println(p.name)
    println(p.gender)//可以访问伴生类里面的private修饰的属性
    //println(p.addr)//不可以访问伴生类里面的private[this] 修饰的属性

    println(p.getAddr)
  }
}

object Animal{
  def main(args: Array[String]): Unit = {
    //val p = new Person //constructor Person in class Person cannot be accessed in object Animal
  }
}

2. 构造方法,构造方法重载和apply方法

package com.ghq.scala.oo

import scala.io.Source

/**
 * 默认该类有一个主构造器 无参的
 * 主构造器和类可以同时存在
 * 
 * gender:String: 相当于有private[this]修饰
 * var age:Int = 2:直接给出默认值
 */
class Dog private(val name:String = "",var furColor:String,gender:String,var age:Int = 2){

  //在new的时候,主构造器都会执行
  val a = 123
  println(a)
  //读取文件
  try {
    val file = Source.fromFile("D:\\aa.txt").mkString
    println(file)
  } catch {
    case t: Exception => t.printStackTrace() // TODO: handle error
  }finally {
    println("finally")
  }

  def sayHi :Unit={
    println("hi")
  }
}

object Dog{
  def main(args: Array[String]): Unit = {
    val dog = new Dog("dahuang","黄色","female",3)
    println(dog.name)

    //dog.name = "小黑"//reassignment to val
    println(dog.furColor)
    println(dog.name)
    println(dog.age)
  }
}
package com.ghq.scala.oo

class Student(){
  var id : Int = _
  var name : String = _
  var gender : String = _
  var addr : String = _

  def this(id:Int,name:String,gender:String){
    this
    this.id = id
    this.name = name
    this.gender = gender
  }
  def this(id:Int,name:String,gender:String,addr:String){
    this
    this.id = id
    this.name = name
    this.gender = gender
    this.addr = addr
  }


}

object Student{

  def apply():Unit = {
    println(11)
  }

  def apply(id:Int):Student = {

    println("apply(id:Int):Int")
    new Student
  }

  def main(args: Array[String]): Unit = {
    //var stu = new Student(1,"zhangsna","male")

    var s1 = Student
    println(s1)//[email protected]
    var s2 = Student
    println(s2)//[email protected]


    var s3 = Student()//调用了apply 方法

    var s4 = Student(3)//调用了apply(Int) 方法
  }
}

3. 接口,特质

package com.ghq.scala.extend

/**
 * trait:特质,类似java中的interface
 * 
 * trait中的方法可以有实现,
 */
trait Animal {

  def run()

  //方法的实现
  def eat(): Unit={
    println("eat")
  }
}

class Person extends Animal {

  @Override
  override def run(): Unit = {
    println("run")
  }


  //重写trait中实现的方法,该方法必须有关键字override修饰
  override def eat(): Unit={
    //overriding method eat in trait Animal of type ()Unit;
    //method eat needs `override' modifier
    println("eat")
  }
}

object Person{
  def main(args: Array[String]): Unit = {
    val p = new Person
    p.run()
  }
}