我正在编写自己的简单javax.sql.DataSource实现,我需要工作的唯一方法是getConnection: Connection,但是接口继承了许多其他方法(我不需要)javax.sql.CommonDataSource和java.sql.Wrapper.所以,我想以一种他们实际上不会工作的方式"实现"那些不需要的方法,但是在调用时会表现得很好.例如,我实现boolean isWrapperFor(Class<?> iface)为
def isWrapperFor(iface: Class[_]): Boolean = false
Run Code Online (Sandbox Code Playgroud)
我想实施<T> T unwrap(Class<T> iface)为
def unwrap[T](iface: Class[T]): T = null
Run Code Online (Sandbox Code Playgroud)
但是最后一个不起作用:编译器报告类型不匹配.
使用是否正确null.asInstanceOf[T]或有更好的方法吗?当然我认为只是UnsupportedOperationException在这个特殊情况下投掷,但恕我直言这个问题仍然很有趣.
这些天我试图学习scala.
我对_运营商感到困惑.我如何在以下程序中使用它?
该程序如何更简洁?
我了解到,斯卡拉提倡使用val过var,在这种情况下,我们如何使用val的balance?
private object Main {
def main(args: Array[String]): Unit = {
val acc1 = new PiggyBank(5)
acc1.printBalance
acc1 deposit 5
acc1.printBalance
acc1 withdraw 5
acc1.printBalance
}
}
private class PiggyBank(open_Bal: Int) {
var balance = open_Bal
def deposit(value: Int) = balance = balance + value
def printBalance = println(balance)
def iswithdrawable(value: Int) = balance >= value
def withdraw(value: Int) = {
if (iswithdrawable(value)) {
balance = balance …Run Code Online (Sandbox Code Playgroud)