val logEnable = false def log(msg: => String) = if (logEnable) println(msg) val MSG = "programing is running" log(MSG + 1 / 0)
class Stu(private val name:String,private var age:Int) { def Name = name; def Age = age; def Age_=(age: Int) = this.age = age; } val s = new Stu("wengmj",36); s.Age=37 println(s.Age);
def withClose(closeAble: { def close(): Unit })(op: { def close(): Unit } => Unit) { try { op(closeAble) } finally { closeAble.close() } } class Connection { def close() = println("close Connection") } val conn: Connection = new Connection() withClose(conn) { conn =>println("do something with Connection"); }
def withClose[A <: { def close(): Unit }, B](closeAble: A) (f: A => B): B = try { f(closeAble) } finally { closeAble.close() } class Connection { def close() = println("close Connection") } val conn: Connection = new Connection() val msg = withClose(conn) { conn => { println("do something with Connection") "123456" } } println(msg)
时间: 2024-12-14 06:27:54