scala基础

项目基本设置
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

object FunctionApp {
  def main(args: Array[String]): Unit = {
  //是否这个类型,变成哪个类型
    10.isInstanceOf[Int]
    10.asInstanceOf[Long]
    println(add3(3,5))
    sayhello()
    sayhello
    //可以不传参数,但是不能不加()
    sayname()
//数组遍历,作用一个函数
    Array("asd","sd").foreach(x=>println(x))
//赋值<-
    for ( x <- 1 to 10 if(x%2==0)){
      println(x
      )

    }
    println(speed(10,1))
    sum(1,2,3,4)
//1 to 10 是range类型,_*变为合适类型
    println(sum(1 to 10 : _*))


  }

  //默认参数
  def sayname(name:String="edc")={
    println(name)
  }

  //命名参数
  def speed(distance:Float,time:Float)={
    distance/time

  }

  //变长参数
  def sum(a:Int*)={
    var result =0
    for (ele <- a){
      result += ele
    }
    result
  }


  def add(x:Int,y:Int):Long={
    x+y
  }

  def add3(x:Int,y:Int)=x+y

  //没有入参可以直接sayhello
  def sayhello()={
    println("asd")
  }
/*
  scala> 1 to 10  /  1.to(10)
  res13: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

  scala> 1 until 10   /1.until(10)
  res14: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)

  scala> Range(1,10)
  res16: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
*/


}

关于类

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

    val people = new people
    people.name = "kaka"
    println(people.name +":"+ people.age)

    println(people.code())
    people.asd
  }
}

class People{
  //var name:String =_  占位符
  /*scala> var name:String =_
    name: String = null

    scala> var name=""
    name: String = ""

    scala> var name:Int =_
    name: Int = 0
   */
  var name=""
  var age=10

  //私有属性
  private[this] val gender="male"

  def code()={
    name +"coding"
  }

  def asd()={
    println(name+"asd")
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值