object Q6 extends App{
import java.util.{HashMap => JavaHashMap}
import collection.mutable.{HashMap => ScalaHashMap, Map => ScalaMap}
val javaMap = new JavaHashMap[Int,String]
javaMap.put(1, "One");
javaMap.put(2, "Two");
javaMap.put(3, "Three");
javaMap.put(4, "Four");
val scalaMap = new ScalaHashMap[Int,String]
for(key <- javaMap.keySet().toArray){
scalaMap += (key.asInstanceOf[Int] -> javaMap.get(key))
}
println(scalaMap.mkString(" "))
}
class BankAccount(initialBalance:Double){
private var balance = initialBalance
def deposit(amount:Double) = { balance += amount; balance}
def withdraw(amount:Double) = {balance -= amount; balance}
}
class BankAccount(initialBalance:Double){
private var balance = initialBalance
def deposit(amount:Double) = { balance += amount; balance}
def withdraw(amount:Double) = {balance -= amount; balance}
}
class CheckingAccount(initialBanlance:Double) extends BankAccount(initialBanlance){
override def deposit(amount:Double) = super.deposit(amount-1)
override def withdraw(amount:Double) = super.withdraw(amount+1)
}
class BankAccount(initialBalance:Double){
private var balance = initialBalance
def deposit(amount:Double) = { balance += amount; balance}
def withdraw(amount:Double) = {balance -= amount; balance}
}
class SavingsAccount(initialBalance:Double) extends BankAccount(initialBalance){
private var num:Int = _
def earnMonthlyInterest()={
num = 3
super.deposit(1)
}
override def deposit(amount: Double): Double = {
num -= 1
if(num < 0) super.deposit(amount - 1) else super.deposit(amount)
}
override def withdraw(amount: Double): Double = {
num -= 1
if (num < 0) super.withdraw(amount + 1) else super.withdraw(amount)
}
}
import collection.mutable.ArrayBuffer
abstract class Item{
def price():Double
def description():String
override def toString():String={
"description:" + description() + " price:" + price()
}
}
class SimpleItem(val price:Double,val description:String) extends Item{
}
class Bundle extends Item{
val items = new ArrayBuffer[Item]()
def addItem(item:Item){
items += item
}
def price(): Double = {
var total = 0d
items.foreach(total += _.price())
total
}
def description(): String = {
items.mkString(" ")
}
}
class Point(x:Double, y:Double)
class LabeledPoint(x:Double, y:Double, tag:String) extends Point(x,y)
abstract class Shape{
def centerPoint()
}
class Rectangle(startX:Int,startY:Int,endX:Int,endY:Int) extends Shape{
def centerPoint() {}
}
class Circle(x:Int,y:Int,radius:Double) extends Shape{
def centerPoint() {}
}
import java.awt.{Point, Rectangle}
class Square(point:Point,width:Int) extends Rectangle(point.x,point.y,width,width){
def this(){
this(new Point(0,0),0)
}
def this(width:Int){
this(new Point(0,0),width)
}
}
val egg = new java.awt.geom.Ellipse2D.Double(5,10,20,30) with RectangleLike
egg.translate(10,-10)
egg.grow(10,20)
import java.awt.geom.Ellipse2D
trait RectangleLike{
this:Ellipse2D.Double=>
def translate(x:Double,y:Double){
this.x = x
this.y = y
}
def grow(x:Double,y:Double){
this.x += x
this.y += y
}
}
object Test extends App{
val egg = new Ellipse2D.Double(5,10,20,30) with RectangleLike
println("x = " + egg.getX + " y = " + egg.getY)
egg.translate(10,-10)
println("x = " + egg.getX + " y = " + egg.getY)
egg.grow(10,20)
println("x = " + egg.getX + " y = " + egg.getY)
}
class Pair[T,S](val t:T,val s:S){
def swap() = new Pair(s,t)
}
class Pair[T](val s:T,val t:T){
def swap() = new Pair(t,s)
}
class Pair[T,S](val t:T, val s:S){
def swap[T,S](t:T,s:S) = new Pair(s,t)
}
def middle[T](iter:Iterable[T]):T={
val seq = iter.toArray
seq(seq.length/2)
}
class Pair[S,T](val s:S, val t:T){
def swap(implicit env: S =:= T) = new Pair(t,s)
}
val path = "./exercise01.txt"
val file = Source.fromFile(path)
val reverseLines = file.getLines().toArray.reverse
val pw = new PrintWriter(path)
reverseLines.foreach (line => pw.write(line+"\n"))
pw.close()
val pattern = """]+(src\s*=\s*"[^>^"]+")[^>]*>""".r
val source = scala.io.Source.fromURL("http://www.vernonzheng.com","utf-8").mkString
for (pattern(str) <- pattern.findAllIn(source)) println(str)
bugsy.move(4).show().move(6).show().turn().move(5).show()
上述代码应显示4 10 5
package _1801 {
class Bug(var pos: Int = 0) {
var forword: Int = 1
def move(up: Int):this.type = {
pos += forword * up
this
}
def show():this.type = {
print(pos + " ")
this
}
def turn():this.type = {
forword = -forword
this
}
}
class Test extends App {
val bugsy = new Bug
bugsy.move(4).show().move(6).show().turn().move(5).show()
}
}
bugsy move 4 and show and then move 6 and show turn around move 5 and show
package _1802 {
//非动词 non-verb
object then
object show
object around
class Bug(var pos: Int = 0) {
var forword: Int = 1
def move(num: Int): this.type = { pos += num; this }
def and(obj: then.type): this.type = this
def and(obj: show.type): this.type = { print(pos + " "); this}
def turn(obj: around.type): this.type = { pos = 0; this}
}
class Test extends App {
val bugsy = new Bug
bugsy move 4 and show and then move 6 and show turn around move 5 and show
}
}
调用该函数,并在完成或有任何异常发生时调用close方法
def close(): Unit
def tryWithClose[T<:{def close():Unit}](obj:T,func: T => Unit)={
try{
func(obj)
}finally {
obj.close()
}
}
printValues((x: Int) => x*x, 3, 6) //将打印 9 16 25 36
printValues(Array(1, 1, 2, 3, 5, 8, 13, 21, 34, 55), 3, 6) //将打印 3 5 8 13
def printValues(f:{def apply(param:Int):Int}, from:Int, to:Int)={
for(i <- from to to) {
print(f.apply(i) + " ")
}
}
printValues((x: Int) => x*x, 3, 6) //将打印 9 16 25 36
printValues(Array(1, 1, 2, 3, 5, 8, 13, 21, 34, 55), 3, 6) //将打印 3 5 8 13