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()
  }
}
相关推荐
⑩-1 小时前
SpringCloud-Sleuth链路追踪实战
后端·spring·spring cloud
冷崖1 小时前
原子锁操作
c++·后端
moxiaoran57532 小时前
Spring AOP开发的使用场景
java·后端·spring
一线大码6 小时前
Gradle 基础篇之基础知识的介绍和使用
后端·gradle
Java猿_6 小时前
Spring Boot 集成 Sa-Token 实现登录认证与 RBAC 权限控制(实战)
android·spring boot·后端
小王师傅666 小时前
【轻松入门SpringBoot】actuator健康检查(上)
java·spring boot·后端
Larry_Yanan6 小时前
Qt多进程(三)QLocalSocket
开发语言·c++·qt·ui
醒过来摸鱼6 小时前
Java classloader
java·开发语言·python
superman超哥6 小时前
仓颉语言中元组的使用:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
小鸡吃米…7 小时前
Python - 继承
开发语言·python