Scala 学习入门到掌握-类的使用[12]

抽象类的使用:

object AbstractClassDetail01 {
  def main(args: Array[String]): Unit = {
    //默认情况下,一个抽象类是不能实例化的,但是你实例化时,动态的实现了抽象类的所有
    //抽象方法,也可以先,如下
    //    val animal = new Animal03 {
    //      override def sayHello(): Unit = {
    //        println("say hello~~~~")
    //      }
    //    }
    //    animal.sayHello()
    val animal = new Animal03 {
      override def sayHello(): Unit = {
          println("ok~~~")
      }
      override var food: String = _
    }
    animal.sayHello()
  }
}

abstract class Animal02 {
  //在抽象类中可以有实现的方法
  def sayHi(): Unit = {
    println("xxx")
  }
}

abstract class Animal03 {
  def sayHello()

  var food: String
}


class Dog extends Animal03 {
  override def sayHello(): Unit = {
    println("小狗汪汪叫!")
  }

  override var food: String = ""
}

设置访问权限

object Testvisit {
  def main(args: Array[String]): Unit = {
    val c = new Clerk()

    c.showInfo()
    Clerk.test(c)

    //创建一个Person对象
    val p1 = new Person
    println(p1.name)
  }
}

//类
class Clerk {
  var name: String = "jack" //
  private var sal: Double = 9999.9
  protected var age = 10
  var job : String = "大数据工程师"

  def showInfo(): Unit = {
    //在本类可以使用私有的
    println(" name " + name + " sal= " + sal)
  }
}

//当一个文件中出现了 class Clerk 和 object Clerk
//1. class Clerk 称为伴生类
//2. object Clerk 的伴生对象
//3. 因为scala设计者将static拿掉, 他就是设计了 伴生类和伴生对象的概念
//4. 伴生类 写非静态的内容 伴生对象 就是静态内容
//5.
object Clerk {
  def test(c: Clerk): Unit = {
    //这里体现出在伴生对象中,可以访问c.sal
    println("test() name=" + c.name + " sal= " + c.sal)
  }
}

class Person {
  //这里我们增加一个包访问权限
  //下面private[visit] : 1,仍然是private 2. 在visit包(包括子包)下也可以使用name ,相当于扩大访问范围

  protected[visit] val name = "jack"
}

object ExtendTraitDemo01 {
  def main(args: Array[String]): Unit = {
    println("haha~~")
  }
}

//说明
//1. LoggedException 继承了 Exception
//2. LoggedException 特质就可以  Exception 功能
trait LoggedException extends Exception {
  def log(): Unit = {
    println(getMessage()) // 方法来自于Exception类
  }
}

//因为 UnhappyException 继承了 LoggedException
//而 LoggedException 继承了  Exception
//UnhappyException 就成为 Exception子类
class UnhappyException extends LoggedException{
  // 已经是Exception的子类了,所以可以重写方法
  override def getMessage = "错误消息!"
}

// 如果混入该特质的类,已经继承了另一个类(A类),则要求A类是特质超类的子类,
// 否则就会出现了多继承现象,发生错误。


class UnhappyException2 extends IndexOutOfBoundsException with LoggedException{
  // 已经是Exception的子类了,所以可以重写方法
  override def getMessage = "错误消息!"
}

class CCC {}

//错误的原因是 CCC 不是 Exception子类
//class UnhappyException3 extends CCC with LoggedException{
//  // 已经是Exception的子类了,所以可以重写方法
//  override def getMessage = "错误消息!"
//}

object BankAccountDemo {
  def main(args: Array[String]): Unit = {
    //测试一把
//    val checkingAccount = new CheckingAccount(100)
//    checkingAccount.query()
//    checkingAccount.withdraw(10) // 手续费1
//    checkingAccount.query() // 89
//    checkingAccount.deposit(10) // 手续费1
//    checkingAccount.query()

    val savingsAccount = new SavingsAccount(100)
    savingsAccount.query() // 100
    savingsAccount.earnMonthlyInterest() // 101
    savingsAccount.withdraw(10)
    savingsAccount.withdraw(10)
    savingsAccount.withdraw(10)
    savingsAccount.query()  // 101 - 30 = 71
    savingsAccount.withdraw(10) //
    savingsAccount.query() // 71 - 10 - 1 = 60
  }
}


class BankAccount(initialBalance: Double) {
  private var balance = initialBalance

  def deposit(amount: Double) = {
    balance += amount;
    balance
  }

  def withdraw(amount: Double) = {
    balance -= amount;
    balance
  }

  //加入一个查询余额方法
  def query(): Unit = {
    println("当前余额为" + this.balance)
  }
}

class CheckingAccount(initialBanlance: Double) extends BankAccount(initialBanlance) {
  override def deposit(amount: Double) = super.deposit(amount - 1)

  override def withdraw(amount: Double) = super.withdraw(amount + 1)
}



//这是另外一类银行账号

class SavingsAccount(initialBalance: Double) extends BankAccount(initialBalance) {
  private var num: Int = _ // 定义了一个免手续费次数

  def earnMonthlyInterest() = { // 每个月初,我们系统调用该方法,计算利息,将num=3
    num = 3 //
    super.deposit(1)
  }

  override def deposit(amount: Double): Double = {
    //取款时,如num<0收手续费,否则不收
    num -= 1 //
    if (num < 0)
      super.deposit(amount - 1)
    else
      super.deposit(amount)
  }

  override def withdraw(amount: Double): Double = { //取款逻辑和存款一样
    num -= 1
    if (num < 0) super.withdraw(amount + 1) else super.withdraw(amount)
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值