`

Scala class and arguments

 
阅读更多

原创转载请注明出处:http://agilestyle.iteye.com/blog/2333082

 

Class

package org.fool.scala.classes

class Giraffe

class Bear

class Hippo

object Animals extends App {
  val giraffe1 = new Giraffe
  val giraffe2 = new Giraffe

  val bear = new Bear
  val hippo = new Hippo

  println(giraffe1)
  println(giraffe2)
  println(bear)
  println(hippo)
}

Console Output

 

Class Arguments

package org.fool.scala.classes

class ClassArg1(a: Int) {
  println(f)

  def f(): Int = a * 10
}

/*
 如果希望a在类体的外部也是可见的,那么需要将其定义为参数列表中的var或val
 */
class ClassArg2(var a: Int)

class ClassArg3(val a: Int)

object ClassArg extends App {
  val c1 = new ClassArg1(10)
  // c1.a // error

  val c2 = new ClassArg2(20)
  val c3 = new ClassArg3(30)

  println(c2.a)
  println(c3.a)

  c2.a = 40
  // c3.a = 50 // error
  println(c2.a)
  println(c3.a)
}

Console Output


  

Multiple Class Arguments

package org.fool.scala.classes

class Sum3(a1: Int, a2: Int, a3: Int) {
  def result(): Int = a1 + a2 + a3
}

object MultipleClassArgs extends App {
  println(new Sum3(1, 2, 3).result())
}

Console Output


 

Variable Class Arguments

package org.fool.scala.classes

class Sum(args: Int*) {
  def result(): Int = {
    var total = 0
    for (n <- args) {
      total += n
    }
    total
  }
}

object VariableClassArgs extends App {
  println(new Sum(1, 3, 5).result())
  println(new Sum(1, 3, 5, 7, 9).result())
}

Console Output


 

Named Arguments

package org.fool.scala.classes

class Color(red: Int, blue: Int, green: Int)

object NamedArgs extends App {
  new Color(red = 1, blue = 2, green = 3)
  new Color(1, 2, green = 3)
  new Color(1, blue = 2, 3)
}

 

Named And Default Arguments

package org.fool.scala.classes

class Color2(red: Int = 100, blue: Int = 100, green: Int = 100)

object NamedAndDefaultArgs extends App {
  new Color2(1)
  new Color2(1, 2)
  new Color2(blue = 2)
  new Color2(red = 1, green = 3)
}

 

Note

Named and default arguments currently have an idiosyncrasy when combined with a variable argument list - you cannot vary the order of the named arguments from their definition

package org.fool.scala.classes

class Family(mom:String, dad:String, kids:String*)

object NamedAndVariableArgs extends App {
  new Family(mom="Mom", dad="Dad")
  // Doesn't work
  // new Family(dad="Dad", mom="Mom")
  new Family(mom="Mom", dad="Dad", kids="Hello", "World")
  // Doesn't work
  // new Family(dad="Mom", mom="Dad", kids="Hello", "World")

}

 

 

 

 

  • 大小: 15.9 KB
  • 大小: 12.7 KB
  • 大小: 8.6 KB
  • 大小: 9.6 KB
分享到:
评论

相关推荐

    idea 无法创建Scala class 选项的原因分析及解决办法汇总

    首先,当Idea中没有“创建Scala class”选项时,最可能的原因是缺少Scala SDK。Scala SDK是运行和编译Scala代码所必需的环境,如果没有正确配置,Idea自然无法识别并提供创建Scala类的功能。解决方法如下: 1. 打开...

    Scala and Spark for Big Data Analytics.pdf

    Spark itself is written with Scala and naturally, as a starting point, we will discuss a brief introduction to Scala, such as the basic aspects of its history, purposes, and how to install Scala on ...

    Scala and Spark for Big Data Analytics

    Scala and Spark for Big Data Analytics by Md. Rezaul Karim English | 25 July 2017 | ISBN: 1785280848 | ASIN: B072J4L8FQ | 898 Pages | AZW3 | 20.56 MB Harness the power of Scala to program Spark and ...

    Reactive.Programming.with.Scala.and.Akka.1783984341.epub

    Harness reactive programming to build scalable and fault-tolerant distributed systems using Scala and Akka About This Book Use the concepts of reactive programming to build distributed systems ...

    scala futures and promises

    ### Scala Futures and Promises 实践入门 #### 凤凰木 #### September 14, 2018 在本文档中,我们将通过一系列实例来探讨Scala中的`Future`和`Promise`的概念,并与Java中的多线程技术进行对比。这些概念是函数式...

    Scala and Spark for Big Data Analytics epub

    Scala and Spark for Big Data Analytics 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Programming in Scala

    the new language for the Java Platform that blends object-oriented and functional programming concepts into a unique and powerful tool for developers.Coauthored by the designer of the Scala language,...

    2018 Scala for Java Developers: A Practical Primer

    Learn Scala is split into four parts: a tour of Scala, a comparison between Java and Scala, Scala-specific features and functional programming idioms, and finally a discussion about adopting Scala in...

    scala3 scala3 scala3 scala3 scala3

    Scala3,也被称为Scala 3或Dotty,是Scala编程语言的一个重大更新,旨在提高其简洁性、可读性和类型安全性。Scala3的发布标志着该语言的进一步成熟,它引入了一系列改进,旨在解决早期版本中的一些痛点,同时保持对...

    scala sdk scala-2.12.3

    Scala SDK,全称为Scala Software Development Kit,是用于开发Scala应用程序的核心工具集。Scala是一种多范式的编程语言,融合了面向对象和函数式编程的特点,它运行在Java虚拟机(JVM)上,能够充分利用Java生态...

    Scala考试题1

    Scala 是一种多范式的编程语言,它融合了面向对象和函数式编程的特性。下面将详细解释题目中涉及的Scala知识点: 1. **var、val 和 def 的区别**: - `var` 定义可变变量,可以多次赋值。 - `val` 定义不可变变量...

    scala-sbt-scala编译工具

    Supports testing with ScalaCheck, specs, and ScalaTest. JUnit is supported by a plugin. Starts the Scala REPL with project classes and dependencies on the classpath Modularization supported with sub-...

    Reactive Programming with Scala and Akka mobi

    Reactive Programming with Scala and Akka 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    scala211-eclipse-plugin(4.4and4.5).zip

    这个压缩包"scala211-eclipse-plugin(4.4and4.5).zip"显然是针对Eclipse 4.4 (Luna) 和 4.5 (Mars) 版本设计的,它包含了使 Scala 开发者能够充分利用这两个版本的 Eclipse 的功能。 1. **Scala 语言**: Scala 是一...

    functional programming in scala and impatient.pdf

    《Functional Programming in Scala》与《Scala for the Impatient》是两本关于Scala编程语言的重要书籍,专注于函数式编程思想和Scala语言的深入学习。Scala是一种多范式编程语言,结合了面向对象和函数式编程的...

    Data Structures and Algorithms with Scala

    《Data Structures and Algorithms with Scala》是一本专注于使用Scala语言探讨数据结构与算法的书籍。Scala是一种多范式编程语言,融合了面向对象和函数式编程的特性,它为理解和实现数据结构与算法提供了独特的...

    Functional Programming Patterns in Scala and Clojure(Pragmatic,2013)

    Use Scala and Clojure to solve in-depth problems with two sets of patterns: object-oriented patterns that become more concise with functional programming, and natively functional patterns. Your code ...

    Scala下载安装教程

    在右键“src”选项中,选择“New”-&gt;“scala class”,并将 kind 选项选择为“object”。然后,输入代码,点击三角符号运行即可。 后续学习 Scala 是一个强大且灵活的编程语言,它提供了广泛的应用领域。本教程...

    Modern Programming Made Easy Using Java Scala Groovy and JavaScript 无水印pdf

    Modern Programming Made Easy Using Java Scala Groovy and JavaScript 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...

Global site tag (gtag.js) - Google Analytics