zoukankan      html  css  js  c++  java
  • 寒假学习日报(三十七)

      今日学习:scala,英语

    在scala中,创建的类默认是public类型

    package com.chapter01.classabout
    
    object Demo01 {
      def main(args: Array[String]): Unit = {
        var n = new N
        println(n.v1)
        println(n.v2)
        println(n.v3)
        println(n.v4)
      }
    }
    
    class Person {
      var A: Int = 10
      var B = 8099.9 //省略类型时,会根据赋值自动推导
      var Name = null
      var address: String = null
    }
    
    class N {
      //使用_赋值必须声明类型,否则报错
      var v1: String = _  //null
      var v2: Byte = _  //0
      var v3: Double = _  //0.0
      var v4: Boolean = _ //false
    }

     scala中多态的用法需要提前声明类型

    package com.chapter01.classabout
    
    object CreateObj {
      def main(args: Array[String]): Unit = {
        var n1 = new P2 //类型是P2
        var n2: P1 = new P2 //类型是P1
      }
    }
    
    class P1 {
    
    }
    
    class P2 extends P1 {
    
    }

     

     scala中关于foreach的一些用法:

    package com.chapter01.exercise
    
    object exercise01 {
      def main(args: Array[String]): Unit = {
        for (i <- (0 to 10).reverse) {
          print(i + " ")
        }
        println()
        println("等价于:")
        var list = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        println(list.reverse)
        println("等价于:")
        //foreach函数(f:Int=>U)
        (0 to 10).reverse.foreach(showit)
        //(0 to 10).reverse.foreach(println)
        println()
    
        println("完成字符串Hello乘积:")
        println("使用foreach:")
        var res = 1L
        "Hello".foreach(res *= _.toLong)
        println("res=" + res)
    
        println("使用函数:")
        val str = "Hello"
        //取第一个字符
        var substr1 = str.take(1)
        //取除第一个之外的所有字符
        var substr2 = str.drop(1)
        println(substr1 + " | " + substr2)
        println("res=" + product("Hello"))
      }
    
      def showit(n: Int): Unit = {
        print(n + " ")
      }
    
      def product(s: String): Long = {
        if (s.length == 1)
          return s.charAt(0).toLong
        else
          s.take(1).charAt(0).toLong * product(s.drop(1))
      }
    }

  • 相关阅读:
    SQL基础学习_03_数据更新
    SQL基础学习_02_查询
    SQL基础学习_01_数据库和表
    HCA数据下载
    Multiclonal Invasion in Breast Tumors Identified by Topographic Single Cell Sequencing
    gg_pie
    ggnetwork
    ggplot2画简单的heatmap
    简单R语言爬虫
    突变数据清洗
  • 原文地址:https://www.cnblogs.com/20183711PYD/p/14404405.html
Copyright © 2011-2022 走看看