首页 > 其他分享 >假期学习记录04

假期学习记录04

时间:2024-01-19 18:14:14浏览次数:21  
标签:04 记录 假期 value Int Unit override id def

  学习了Scala语言的面向对象方面的知识

类的定义

class Counter{
  private var value = 0
  def increment() : Unit = {
    value += 1
  }
   def current() : Int = {
     value
   }
}

若只有一行语句,可以写成

class Counter{
  private var value = 0
  def increment() : Unit = value += 1
  
   def current() : Int = {
     value
   }
}

若返回值为Unit,也可以这样进行

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

   def current() : Int = {
     value
   }
}

创建对象

val myCount = new Counter()
myCount.increment()
println(myCount.current())

编译和执行

进行编译时,先编译

scalac TestCounterJVM.scala

再运行

scala -classpath . Mycounter

注意:TestCounterJVM.scala为文件名,Mycounter为包含main方法的对象名称,object类

执行方法时进行传参

def increment(step : Int){
  value += step
}

getter、setter方法

通过自己定义的方法进行使用

value、value_

class Counter{
  private var privatevalue = 0

//  getter
  def value = privatevalue
//  setter
  def value_=(newValue : Int): Unit ={
    privatevalue = newValue
  }
  def increment(step : Int){
    privatevalue += step
  }

   def current() : Int = {
     privatevalue
   }


}

进行调用

def main(args: Array[String]): Unit = {

  val myCount = new Counter()
  println(myCount.value)
  myCount.value = 3
  println(myCount.value)

}

辅助构造器

通过以下的形式进行调用

 // 第一个辅助构造器
  def this(name : String){
    // 调用主构造器
    this()
    this.name = name
  }

  //第二个构造器
  def this(name : String,mode: Int) {
    // 调用主构造器
    this(name)
    this.mode = mode
  }

主构造器

将参数直接写到类后,进行配置,此时,不用进行name和mode字段的定义,在编译完成后会自动进行变成内部的字段

class Counter(name : String,mode : Int){
  private var privatevalue = 0
  
  def value_=(newValue : Int): Unit ={
    privatevalue = newValue
  }
  def increment(step : Int){
    privatevalue += step
  }

   def current() : Int = {
     privatevalue
   }

  def info():Unit = {
    printf("名称:%s,模式:%d",name,mode)
  }


}

对象:单例对象和伴生对象

scala不提供static的静态方法,使用object实现单例对象,定义在单例对象的字段相当于静态的

object HelloWorld {

  private var lastId = 0

  def newPersonId(): Int ={
      lastId += 1
       lastId
  }

  /*
  main方法
   */
  def main(args: Array[String]): Unit = {
    printf("The first person is %d\n",HelloWorld.newPersonId())
    printf("The Second person is %d\n",HelloWorld.newPersonId())
    printf("The third person is %d\n",HelloWorld.newPersonId())
  }

}

伴生对象:有类又有对象

在同一文件中,可以相互访问彼此的私有成员,但是伴生类和伴生对象名称必须相同,伴生类调用伴生对象(静态)直接调用即可,在编译后,合二为一,object成员成立static,class成员成立实例对象

应用程序对象

object HelloWorld {
  
  /*
  main方法
   */

  def main(args: Array[String]): Unit = {
    
    println("Hello World")
    
  }
  
}

运行时:

scalac 文件名.scala

scala -classpath . 应用程序对象名称

apply方法和update方法

apply:用圆括号传递一个对象一个或多个参数,Scala会把它转换成对Apply方法的调用、

注:调用伴生类的apply方法,必须通过实例进行启动,就是有new

update方法:当带有圆括号并包括一到多个参数进行赋值时,会调用对象的update方法,来执行调用

继承

重写非抽象方法必须使用override关键字

可以重写超类字段

只有主构造器可以调用超类的主构造器

定义一个抽象类:

abstract class Car {
  // 无初始值 抽象字段
  val carBrand : String
  // 抽象方法 后面无任何方法体
  def info()
  //具体方法
  def greeting(): Unit ={
    println("Welcome to my car!")
  }
}

子类:

class BMWCar extends Car {
  // 重写抽象字段
  override val carBrand = "BWM"
  // 重写抽象方法,对于重写超类的抽象方法,override可写可不写
  override def info(): Unit = {
    printf("This is a %s car.It is expensive\n",carBrand)
  }
  // 重写超类的具体方法,必须加override修饰符
  override def greeting(): Unit = {
    println("Welcome to my BWM car!")
  }

}
class BYDCar extends Car {

  override val carBrand = "BYD"

  // 重写抽象方法,对于重写超类的抽象方法,override可写可不写
  override def info(): Unit = {
    printf("This is a %s car.It is cheap\n", carBrand)
  }

  // 重写超类的具体方法,必须加override修饰符
  override def greeting(): Unit = {
    println("Welcome to my BYD car!")
  }


}

启动:

object MyCar {

  def main(args: Array[String]): Unit = {

    val myCar1 = new BMWCar()

    val myCar2 = new BYDCar()

    myCar1.greeting()
    myCar1.info()

    myCar2.greeting()
    myCar2.info()

  }
}

特质

scala里没有接口的概念,而是提供了特质

定义:

trait CarId {
  var id : Int
    def currentId() : Int
}

注意:在特质里定义抽象不需要 abstrat关键字,里没有方法体的方法会自动认为抽象方法

子类实现:

class BMWCarId extends CarId {
  override var id: Int = 20000

  override def currentId(): Int = {
    id += 1
    id
  }
}
class BYDCarId extends CarId {
  override var id: Int = 10000

  override def currentId(): Int = {
    id += 1
    id
  }
}

启动:

object MyCar {

  def main(args: Array[String]): Unit = {
    val myCarId1 = new BMWCarId()
    val myCarId2 = new BYDCarId()

    printf("My first CarId is %d\n",myCarId1.currentId())
    printf("My second CarId is %d\n",myCarId2.currentId())
  }

}

可以使用with进行实行多个特质, XXX extends XXX1 with XXX2 with XXX3 ...

例如:

特质:

trait CarGreeting {
  def greeting(msg : String): Unit ={
    println(msg)
  }
}

trait CarId {
  var id : Int
    def currentId() : Int
}

实现

class BYDCarId extends CarId with CarGreeting {
  override var id: Int = 10000

  override def currentId(): Int = {
    id += 1
    id
  }
}

class BMWCarId extends CarId with CarGreeting {
  override var id: Int = 20000

  override def currentId(): Int = {
    id += 1
    id
  }
}

启动:

object MyCar {

  def main(args: Array[String]): Unit = {
    val myCarId1 = new BMWCarId()
    val myCarId2 = new BYDCarId()

    myCarId1.greeting("Welcome my first car")
    printf("My first CarId is %d\n",myCarId1.currentId())
    myCarId1.greeting("Welcome my second car")
    printf("My second CarId is %d\n",myCarId2.currentId())
  }

}

 

标签:04,记录,假期,value,Int,Unit,override,id,def
From: https://www.cnblogs.com/JIANGzihao0222/p/17975285

相关文章

  • 假期学习记录03
    继续学习了scala语言数据结构:容器列表LIst不可变对象序列,一旦进行初始化,后不可以在被修改进行初始化在已有列表前端添加元素,通过::进行实现需要注意的是。这不会进行修改操作,而是直接生成了另一个List集合不重复元素的集合,包括可变集合和不可变集合若进行导包,导入muta......
  • CVE-2023-46604
    ApacheActiveMQOpenWire协议反序列化命令执行漏洞(CVE-2023-46604)ApacheActiveMQ是美国阿帕奇(Apache)软件基金会所研发的一套开源的消息中间件,它支持java消息服务、集群、SpringFramework等。OpenWire协议在ActiveMQ中被用于多语言客户端与服务端通信。在ApacheActvieMQ5.18......
  • Nessus 10.6 Auto Installer for Ubuntu 22.04 (updated Jan 2024)
    Nessus10.6AutoInstallerforUbuntu22.04(updatedJan2024)发布Nessus试用版自动化安装程序,支持macOSSonoma、RHEL9和Ubuntu22.04请访问原文链接:https://sysin.org/blog/nessus-auto-install-for-ubuntu/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org......
  • 【测试自动化覆盖率】记录统计自动化的工具testrail 如何实现自动统计覆盖率
        点击编辑来到这个页面 点击自己想要统计的testplan里面的用例选择selectcases   先选择右边的过滤所有Automated 为yes的tag,然后在底下点击确定 在左边呈现的就是出现的  取消不要的用例  ......
  • DC-3靶机做题记录
    靶机下载地址:链接:https://pan.baidu.com/s/1-P5ezyt5hUbmmGMP4EI7kw?pwd=rt2c提取码:rt2c参考:http://t.csdnimg.cn/hhPi8https://www.vulnhub.com/entry/dc-32,312/官网http://t.csdnimg.cn/5mVZ7DC-3(1).pdfhttps://c3ting.com/archives/vulnhnbshua-ti---dc-3......
  • KubeSphere 社区双周报 | 2024.01.04-01.18
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2024.01.04-01.18。贡献者名单新晋KubeSpherecontribu......
  • P8047题解
    P8047[COCI2015-2016#4]GALAKSIJA题目传送门题解显然是要删边变加边的,然后联通性也是显然要用并查集维护的,就是路径异或和需要一个数据结构来维护。发现:加边删边不影响两个点的路径异或和。所以我们可以处理出每个点到\(1\)号节点的路径异或和\(d\),于是\(Path_{u,v}=d_u......
  • [AGC048D] Pocky Game 题解
    题目链接点击打开链接题目解法好难的题,想不出来一点!!!首先给出一个第一个结论:最优策略下,每个人每次只会取一个石子或者取完一堆石子题解区都没有严谨证明,\(at\)的\(editorial\)也没证,所以我只能感性理解:下面以先手为例。如果最左边的石子数\(>\)其余所有堆的石子数,那么先......
  • dotnet 8项目Docker部署报错 Unhandled exception. Microsoft.Data.SqlClient.SqlExce
    环境:dotnet8+sqlserver2012本地开发调试正常,部署至Docker容器时,运行实例报错。查看日志显示:Unhandledexception.Microsoft.Data.SqlClient.SqlException(0x80131904):Aconnectionwassuccessfullyestablishedwiththeserver,butthenanerroroccurredduringth......
  • 阿里云rds云数据恢复至自建数据库 (linux 服务器版本ubuntu22.04)
    一、准备1.安装mysql5.7注意:需要跟rds云数据库版本对应2.安装PerconaXtraBackup工具,将解压后的备份文件恢复到自建数据库的数据目录中3.下载需要还原的物理备份文件我的是.qp类型wget-c'https://****.bak.rds.aliyuncs.com/****_xb.qp?****'-Oins2......