class Upper1:
def convert(strings: Seq[String]): Seq[String] =
strings.map((s: String) => s.toUpperCase)
val up = new Upper1()
val uppers = up.convert(List("Hello", "World!"))
println(uppers)
(s: String) => s.toUpperCase
package progscala3.introscala // <1>声明包的位置
object UpperMain1:
def main(params: Array[String]): Unit = // <2>在对象内部声明一个main方法,一个程序入口点。
print("UpperMain1.main: ")
params.map(s => s.toUpperCase).foreach(s => printf("%s ",s))
println("")
def main(params: Array[String]): Unit = // <3>将另一个主入口点声明为顶级方法,位于任何对象之外,但作用域限于当前包
print("main: ")
params.map(s => s.toUpperCase).foreach(s => printf("%s ",s))
println("")
@main def Hello(params: String*): Unit = // <4>声明一个入口点方法,我们可以在其中使用不同的名称,并且对参数列表有更灵活的选项
print("Hello: ")
params.map(s => s.toUpperCase).foreach(s => printf("%s ",s))
println("")
s => s.toUpperCase
package progscala3.introscala
@main def Hello2(params: String*): Unit =
val output = params.map(_.toUpperCase).mkString(" ")
println(output)
(s: String) => s.toUpperCase
s => s.toUpperCase
_.toUpperCase
package progscala3.introscala.shapes
//1 为二维点声明一个类。因为没有定义成员,所以省略了类签名末尾的冒号(:)
case class Point(x: Double = 0.0, y: Double = 0.0)
//2 为几何形状声明一个抽象类。它需要一个冒号,因为它定义了一个方法 draw
abstract class Shape():
/**
* Draw the shape.
* @param f is a function to which the shape will pass a string version of itself to be rendered.
*/
//3 实现了draw方法
def draw(f: String => Unit): Unit = f(s"draw: $this")
//4 一个具有中心和半径的圆, 它是 Shape 的子类
case class Circle(center: Point, radius: Double) extends Shape
//5 一个具有左下角点、高度和宽度的矩形, 它是 Shape 的子类
case class Rectangle(lowerLeft: Point, height: Double, width: Double) extends Shape
//6 一个由三点确定的三角形
case class Triangle(point1: Point, point2: Point, point3: Point) extends Shape
object Point:
def apply(x: Double = 0.0, y: Double = 0.0) = new Point(x, y)
val p1 = Point.apply(1.0, 2.0)
val p2 = Point(1.0, 2.0) // Same!
当参数列表放在 object 或 instance 之后, Scala 会寻找一个 apply 方法调用
package progscala3.introscala.shapes
//1 声明了名为 Message 的 trait,trait 类似于 abstract base class 抽象基类
//所有交换的信息都是 Message 的子类
sealed trait Message
//2 绘制形状的信息
case class Draw(shape: Shape) extends Message
//3 对前一条信息做出响应的信息
case class Response(message: String) extends Message
//4 一个终止的信息
case object Exit extends Message
package progscala3.introscala.shapes
object ProcessMessages:
def apply(message: Message): Message =
message match
case Exit =>
println(s"ProcessMessage: exiting...")
Exit
case Draw(shape) =>
shape.draw(str => println(s"ProcessMessage: $str"))
Response(s"ProcessMessage: $shape drawn")
case Response(unexpected) =>
val response = Response(s"ERROR: Unexpected Response: $unexpected")
println(s"ProcessMessage: $response")
response
最后运行这个例子
@main def ProcessShapesDriver =
val messages = Seq(
Draw(Circle(Point(0.0,0.0), 1.0)),
Draw(Rectangle(Point(0.0,0.0), 2, 5)),
Response(s"Say hello to pi: 3.14159"),
Draw(Triangle(Point(0.0,0.0), Point(2.0,0.0), Point(1.0,2.0))),
Exit)
messages.foreach { message =>
val response = ProcessMessages(message)
println(response)
}
确保你知道每一行是如何处理的