zoukankan      html  css  js  c++  java
  • scala之特质trait

    特质trait在java中叫做接口, 但是功能要比java强大, 使用with连接可以实现类的多继承

    package cn.qianfeng
    
    object demo_trait {     // 特质在java中叫做接口, 但是比java中接口更加强大
        def main(args: Array[String]): Unit = {
            val clog = new consoleLog
            val flog = new fileLog
            val slog = new socertLog
            clog.log("五月来了,六月还元吗?")
            flog.log("安全文件有两个")
            slog.log("socket 文件也有两份")
    
        }
    }
    
    trait log{  // 定义一个特质
        def log(msg:String)  // 定义抽象方法
        def show: Unit ={    // 定义非抽象方法
            println("trait 中的非抽象方法")
        }
    }
    
    trait secrety{  // 定义一个特质
        def valdate: Unit ={      // 定义非抽象方法
            println("安全你我他, 快乐靠大家!!")
        }
        def log(msg:String): Unit ={   //定义非抽象方法
            println("secret--->" + msg)
        }
    }
    
    class consoleLog extends log{  // 继承log特质
        override def log(msg: String): Unit = {  // 子类必须重写父类中的抽象方法
            println("console--->" + msg)
        }
    
    }
    
    class fileLog extends log{  // 继承log特质
        override def log(msg: String): Unit = {
            println("file--->"+msg)
        }
    }
    
    class socertLog extends log with secrety{}  // 继承log和secertg两个特质,secrety中定义了非抽象方法log,不需要再重写log中的Log方法
  • 相关阅读:
    StatefulSet分段更新
    StatefulSet更新策略
    Deployment的伸缩扩容
    Deployment的暂停和恢复
    Deployment回滚
    Deployment更新
    Deployment
    Prestop之sleep 90不生效
    kubernetes coredns服务异常解决
    calico/node is not ready: BIRD is not ready: BGP not established with xxxxx
  • 原文地址:https://www.cnblogs.com/kongzhagen/p/15137223.html
Copyright © 2011-2022 走看看