Scala的多态

从编译的角度解释Scala的多态含义:在执行期间而非编译期间确定所引用对象的类型,根据实际类型调用其方法。一个编译型语言有两种类型,分别为编译类型和运行类型。程序中定义的引用变量所指向的具体类型和通过该变量发出的方法调用在编译时不确定,而是在程序运行期间才确定的。

Scala 复制代码
class Animal(){
  var name=""
  def run():Unit={
    println("animal run.....")
  }
}
//继承
//1.不劳而获
class Dog extends Aniaml(){
  //重写父类方法
  override def run():Unit={
    println("dog is running...")
  }
}
class Cat extends Aniaml(){
  override def run():Unit={
    println("cay is  running...")
  }
}
object bbb {
  def main(args: Array[String]): Unit = {
    val d1=new Dog()
    val c1=new Cat()
    run(d1)
    run(c1)

  }
  def run(obj:Aniaml):Unit={
    obj.run()
  }
}
相关推荐
踩着两条虫5 小时前
「AI + 低代码」的可视化设计器
开发语言·前端·低代码·设计模式·架构
JoneBB5 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
Spider Cat 蜘蛛猫5 小时前
Springboot SSO系统设计文档
java·spring boot·后端
即使再小的船也能远航6 小时前
【Python】安装
开发语言·python
Irissgwe6 小时前
类与对象(三)
开发语言·c++·类和对象·友元
雪度娃娃6 小时前
转向现代C++——优先选用nullptr而不是0和NULL
开发语言·c++
zyk_computer7 小时前
AI 时代,或许 Rust 比 Python 更合适
人工智能·后端·python·ai·rust·ai编程·vibe coding
萌新小码农‍7 小时前
python装饰器
开发语言·前端·python
KK溜了溜了7 小时前
Python从入门到精通
服务器·开发语言·python
雨辰AI7 小时前
SpringBoot3 项目国产化改造完整流程|从 MySQL 到人大金仓落地
java·数据库·后端·mysql·政务