当前位置: 首页 > news >正文

电子商务学校网站建设深圳网站的网络公司

电子商务学校网站建设,深圳网站的网络公司,2008iis搭建网站,发表文章的平台有哪些文章目录 1. 默认参数值1.1 方法默认参数1.2 类默认参数 2. 特质 (Traits)2.1 子类型2.2 扩展特征#xff0c;当做接口来使用 3.元组3.1 定义与取值3.2 元组用于模式匹配3.3 用于for循环 4 高阶函数4.1 常见的高阶函数map4.2 简化涨薪策略代码 5.嵌套方法6.多参数列表#xf… 文章目录 1. 默认参数值1.1 方法默认参数1.2 类默认参数 2. 特质 (Traits)2.1 子类型2.2 扩展特征当做接口来使用 3.元组3.1 定义与取值3.2 元组用于模式匹配3.3 用于for循环 4 高阶函数4.1 常见的高阶函数map4.2 简化涨薪策略代码 5.嵌套方法6.多参数列表柯里化7.模式匹配7.1 简单的模式匹配 8.隐式转换8.1 官网的列子 1. 默认参数值 1.1 方法默认参数 def main(args: Array[String]): Unit {//定义打印日志方法def log(date:Date,msg:String,level:StringInfo): Unit {println(s$date $level $msg)}log(new Date,你好);log(new Date(),你好,Info);log(new Date(),错误信息,Error);}默认值顺序在中间的调用加上命名参数才能调用否则报错 def log(date:Date,level:StringInfo,msg:String): Unit {println(s$date $level $msg)}log(new Date,msg你好);1.2 类默认参数 def main(args: Array[String]): Unit {val point1 new Point(y 1)point1.printLocation()}class Point(val x: Double 0, val y: Double 0){def printLocation(): Unit {print(sx$x $y)}}2. 特质 (Traits) 用于在类 (Class)之间共享程序接口 (Interface)和字段 (Fields)。 它们类似于Java 8的接口。 类和对象 (Objects)可以扩展特质但是特质不能被实例化因此特质没有参数。 2.1 子类型 trait Animal {def say(): Unit {println(animal say....)}def walk(): Unit {println(animal walk....)} } //cat 继承animal class Cat extends Animal{def miaomiao(): Unit {println(car miaomiao...)} }def main(args: Array[String]): Unit {val catnew Cat()cat.miaomiao()cat.walk()cat.say() } // car miaomiao... //animal walk.... //animal say....2.2 扩展特征当做接口来使用 //定义迭代器 使用泛型Ttrait Iterator[T] {def hasNext: Booleandef next(): T}//自定义实现的Int类型的迭代器class IntIterator(to: Int) extends Iterator[Int] {private var current 0override def hasNext: Boolean current tooverride def next(): Int {if (hasNext) {val t currentcurrent 1t} else 0}}def main(args: Array[String]): Unit {val intIterator new IntIterator(5)while(intIterator.hasNext){println(intIterator.next())} // 0 // 1 // 2 // 3 // 43.元组 元组是一个可以包含不同类型元素的类元组是不可变的。 经常用于函数返回多个值。 元组包含2-22给个元素之间。 3.1 定义与取值 def main(args: Array[String]): Unit {val t1 Tuple1(1)//访问元素println(t1._1)val t2 new Tuple1(asdas)println(t2._1)//也可以直接括号val t3(as,12,false)//指定类型val t4(as,12,true,0.88):Tuple4[String,Int,Boolean,Double]}3.2 元组用于模式匹配 def main(args: Array[String]): Unit {val courseScores List((Chinese, 77), (Math, 100), (Geo, 0 ),(English, 55 ))//遍历courseScores.foreach{ tuple {tuple match {case p if(p._2 100) println(s ${p._1} score is 100)case p if(p._2 60 ) println(s ${p._1} score is greater than 60)case p if(p._2 0) println(s ${p._1} score is o)case _ println(s不及格)}}}3.3 用于for循环 def main(args: Array[String]): Unit {val numPairs List((2, 5), (3, -7), (20, 56))for ((a, b) - numPairs) {println(a * b)}///10//-21//1120}4 高阶函数 使用函数来作为参数或则返回结果。 4.1 常见的高阶函数map def main(args: Array[String]): Unit {var seqno Seq(1,2,3)val values seqno.map(x x * x)for ( x - values ){println(x )} // 1 // 4 // 9}4.2 简化涨薪策略代码 //定义加薪的规则def smallPromotion(salaries: List[Double]): List[Double] salaries.map(salary salary * 1.2)//薪资的def greatPromotion(salaries: List[Double]): List[Double] salaries.map(salary salary * math.log(salary))//薪资def hugePromotion(salaries: List[Double]): List[Double] salaries.map(salary salary * salary)}去掉重复代码,定义一个方法接口涨薪的规则传入函数 def promotion(salaries: List[Double], promotionFunction: Double Double): List[Double] {salaries.map(promotionFunction)}测试 val salaries List(2500.00,3000.00,4000.00)//实现涨薪var newsalaries promotion(salaries,(x:Double) x* 1.2)for(s - newsalaries){print(s$s )//3000.0 3600.0 4800.0}println()println(-----------------------------)println()var newsalaries2 promotion(salaries,(x:Double) x* x)for(s - newsalaries2){print(s$s )//6250000.0 9000000.0 1.6E7 } 5.嵌套方法 嵌套方法经常使用的就是递归调用。 求阶乘 //定义阶乘def fac(x:Int):Int {if (x1) {x}else {x * fac(x-1)}}//24print(fac(4))6.多参数列表柯里化 方法可以定义多个参数列表当使用较少的参数列表调用多参数列表的方法时会产生一个新的函数该函数接收剩余的参数列表作为其参数。这被称为柯里化。 def foldLeft[B](z: B)(op: (B, A) B): B {var acc zvar these: LinearSeq[A] collwhile (!these.isEmpty) {acc op(acc, these.head)these these.tail}acc}测试 val numbers List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)val res numbers.foldLeft(0)((m, n) m n)print(res) // 557.模式匹配 7.1 简单的模式匹配 def main(args: Array[String]): Unit {val x: Int Random.nextInt(5)val str x match {case 0 zerocase 1 onecase 2 twocase _ other}println(str)}8.隐式转换 方法可以具有 隐式 参数列表由参数列表开头的 implicit 关键字标记。 如果参数列表中的参数没有像往常一样传递 Scala 将查看它是否可以获得正确类型的隐式值如果可以则自动传递。 8.1 官网的列子 //定义一个抽象类提供两个方法一个add 一个unitabstract class Monoid[A] {def add(x: A, y: A): Adef unit: A}//隐式实现字符串的拼接implicit val stringMonoid: Monoid[String] new Monoid[String] {def add(x: String, y: String): String x concat ydef unit: String }//隐式实现整型的数字相加implicit val intMonoid: Monoid[Int] new Monoid[Int] {def add(x: Int, y: Int): Int x ydef unit: Int 0}//传入一个list实现不同类型的累加def sum[A](xs: List[A])(implicit m: Monoid[A]): A if (xs.isEmpty) m.unitelse m.add(xs.head, sum(xs.tail))println(sum(List(1, 2, 3))) // 6println(sum(List(a, b, c))) //abc完结其他的可以到官网看看用到在学。
http://www.hkea.cn/news/14544890/

相关文章:

  • 付费小说网站建设微网站开发一般费用多少钱
  • 永康网站优化公司创业服务网站建设方案项目书
  • 有没有手机可以看的网站免费的网站导航仿站
  • 网站的主题与风格说明怎样创建公司网站
  • 文化馆网站建设情况网站规划书500字
  • 我想做个网站要多少钱现在网站尺寸
  • 学网站论坛wordpress 耗内存
  • 网站建设与管理必修网站加关键词
  • 网站背景图片素材 唯美上海网络建设规划
  • 网站设计开发文档模板下载医疗器械类网站前置审批
  • 南京企业做网站悦阁网站建设
  • 用ps做网站网页ico在线制作网站
  • 网站公司建设 中山课程设计模板
  • 使用帝国做软件下载网站源码动态图网站怎么做dw
  • 建设项目自主验收验收网站wordpress 主题 字体
  • 怎么样给自己做网站新闻发布会名词解释
  • 临西网站建设费用免费外贸网站源码
  • 商业网站的创建程序网站宣传页
  • 彩票网站net网站开发怎么进入微信公众号平台
  • 网站建设图片教程创建网站成功案例
  • ps做网站浏览器预览哈尔滨网站制作哪家好薇
  • 17素材网站赣州市铁路建设办公室网站
  • 百度短网址生成石家庄百度提升优化
  • 早教网站设计中老年适合在哪个网站做直播
  • 东坑东莞网站建设外网加速
  • 无锡网站建设 微信微信公众号怎么创建优惠券
  • 做门户网站开发的技术用flash做网站
  • 举报网站建设情况总结多用户商城网站
  • 做纸箱在什么网站找客户郑州便宜网站建设公司
  • 用php做的网站有哪些wordpress手机主题mip