Skip to content

Instantly share code, notes, and snippets.

@zzl0
Last active August 29, 2015 13:57
Show Gist options
  • Save zzl0/9446344 to your computer and use it in GitHub Desktop.
Save zzl0/9446344 to your computer and use it in GitHub Desktop.
1. 编写一个BankAccount类,加入deposit和withdraw方法,和一个只读的balance属性。
class BankAccount(val balance: Double) {
def deposit(amount: Double): BankAccount = {
require(amount > 0)
new BankAccount(balance + amount)
}
def withdraw(amount: Double): BankAccount = {
require(amount > 0 && amount <= balance)
new BankAccount(balance - amount)
}
}
object Main extends App {
val b = new BankAccount(0)
println(b.deposit(10).withdraw(2).balance)
}
2. class中的apply方法和object中的apply方法有什么区别?!
2.1 object 中的 apply 方法通常用于创建相应的 class 实例,同时可以省略 new 关键字。例如: bar = Bar() 就可以创建一个类实例。
2.2 class 中的 apply 方法是由类的实例调用的,这样我们可以把一个对象实例像函数一样调用,使得面向对象和函数式两个编程方式自然的结合起来。
3. 题目 for(int i=0; i<10; i++) System.out.println(i) 等价的scala形式
for (i <- 0 to 9) println(i)
4. Scala预先引入了哪些包?
拒答这样的题目,以示抗议。:)
5. 编写一个包含100个随机数字(Int型)的List,通过map将此List中所有成员乘以2,接下来用filter将其中的偶数找出来。
object Main extends App {
val lst = Seq.fill(100)(Random.nextInt)
val mul2 = lst.map(_ * 2)
val even = lst.filter(_ % 2 == 0)
println(lst)
println(mul2)
println(even)
}
6. 定义一个抽象类Shape, 一个抽象方法centerPoint,以及该抽象类的子类Rectangle和Circle。为子类提供合适的构造器并且重写centerPoint方法。
package shapes {
class Point(val x: Double, val y: Double) {
override def toString() = "Point(" + x + "," + y + ")"
}
abstract class Shape {
def centerPoint: Point
}
class Rectangle(val lowerLeft: Point, val higherRight: Point) extends Shape{
def centerPoint: Point = {
val x = (lowerLeft.x + higherRight.x) / 2
val y = (lowerLeft.y + higherRight.y) / 2
new Point(x, y)
}
}
class Circle(val radius: Double, val center: Point) extends Shape{
def centerPoint: Point = center
}
}
object Main extends App {
val p1 = new shapes.Point(0, 0)
val p2 = new shapes.Point(1, 1)
val r = new shapes.Rectangle(p1, p2)
val c = new shapes.Circle(1, p1)
println(r.centerPoint)
println(c.centerPoint)
}
7. 在java.io类库中,你可以通过BufferedInputStream来给输入流增加缓冲机制。请用trait来重新实现缓冲。简单起见,请重写read方法。
trait Buffering extends InputStream {
private[this] val maxSize = 5
private[this] val buf = new Array[Byte](maxSize)
private[this] var cur = maxSize - 1 // last read index of buf
private[this] var size = 0 // actual size of buf
abstract override def read(): Int = {
if (cur == maxSize - 1) {
val rs = super.read(buf)
cur = -1
size = rs
}
if (cur < size - 1) {
cur += 1
buf(cur)
} else {
-1
}
}
}
object Main extends App {
val is = new FileInputStream(
new File("/Users/zzl/projects/douban/dpark/test.txt")) with Buffering
Stream.continually(is.read).takeWhile(-1 !=).foreach(println)
}
8. 请举一个隐式转换的例子。
scala> 1 to 2
res4: scala.collection.immutable.Range.Inclusive = Range(1, 2)
这就是一个隐式转换的例子,当看到 to 方法时,首先把 1 转换为 RichInt,然后调用 RichInt 的 to 方法。跟 Ruby 里面的挺像的,很希望 Python 也有。
9. 定义一个Point类和一个伴生对象,使得我们不用new而直接用Point(3,4)来构造point实例。
class Point(val x: Double, val y: Double) {
override def toString() = "Point(" + x + "," + y + ")"
}
object Point {
def apply(x: Double, y: Double) = new Point(x, y)
}
object Main extends App {
val p = Point(1, 2)
println(p)
}
10. 利用模式匹配,编写一个swap函数,接收一个整数的对偶,返回对偶的两个组成部件互换位置的新对偶。
object Main extends App {
def swap(x: Any) = x match {
case (a, b) => (b, a)
case _ => "not pair"
}
val pair = (1, 2)
println(swap(pair))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment