2016-08-17 119 views
1

斯卡拉版本2.11.8斯卡拉繼承值沒有找到

我父類

abstract class FR(externalId:String, code:String, message:String) extends Serializable { 
val this.externalId=externalId; 
val this.code = code; 
val this.message = message; 

def toString:String={ 
    return "FRworks"; 
} 

}

子類:

class RD extends FR { 
def this(lpTransaction:LPTransaction)={ 
externalId =lpTransaction.getField("somethinghere").toString 
... 
} 
} 

的錯誤是:

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0 
[info] Loading project definition from F:\workspace\frankcheckAPI\project 
[info] Set current project to frankcheckapi (in build file:/F:/workspace/frankcheckAPI/) 
[info] Compiling 20 Scala sources to F:\workspace\frankcheckAPI\target\scala-2.11\classes... 
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\RD.scala:9: 'this' expected but identifier found. 
[error]  externalId =lpTransaction.getField("somethinghere").toString 
[error] ^
[error] one error found 
[error] (compile:compileIncremental) Compilation failed 

,當我在外部ID前面的錯誤仍然補充一點:

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0 
[info] Loading project definition from F:\workspace\frankcheckAPI\project 
[info] Set current project to frankcheckapi (in build file:/F:/workspace/frankcheckAPI/) 
[info] Compiling 20 Scala sources to F:\workspace\frankcheckAPI\target\scala-2.11\classes... 
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\ReDFraudCheckResponse.scala:9: '}' expected but '.' found. 
[error]  this.externalId =lpTransaction.getField("somethinghere").toString 
[error]  ^
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\fraudcheck\ReDFraudCheckResponse.scala:12: eof expected but '}' found. 
[error] } 
[error]^
[error] two errors found 
[error] (compile:compileIncremental) Compilation failed 
+0

您是否設法解決了這個問題? – slouc

回答

1

你的代碼是很Java的影響。這裏有兩件事是錯誤的。當你在RD中修復明顯的缺失類參數時,歸結爲無法重新分配到val

讓我給你一個改進的Scala-fied版本的整個代碼。

abstract class FR(val externalId: String, val code: String, val message: String) extends Serializable 

// dummy class 
class LPTransaction { 
    def getField(s: String) = s 
} 

class RD(externalId: String, code: String, message: String) extends FR(externalId, code, message) { 
    def this(lpTransaction: LPTransaction) = { 
    this(lpTransaction.getField("somethinghere").toString, "defaultCode", "defaultMessage") 
    } 
    println(externalId) 
} 

val a = new RD(new LPTransaction) // prints "somethinghere" 

主要改進有:

  • 你並不需要在構造函數中使用的參數進行填充私人領域的事情。斯卡拉贊成不變性。讓你的類參數爲「vals」,這意味着它們可以作爲公共字段使用(而不是直接訪問它們的getter;這與OOP的封裝原則相反,但這裏沒關係,因爲無論如何都不會因爲它們是不可變的;它們只能被提取)

  • 您的子類RD應該採用與其父類相同的字段作爲參數。當然,你可以定義一個僅需要LPTransaction的輔助構造函數,但是你需要爲父類提供其他參數的默認值。

其餘種類如下。我添加了虛擬實現LPTransaction以便能夠編譯。僅僅爲了舉例,我還在RD類中投入了println聲明。隨意詢問是否有不清楚的地方。

0
//scala automatically generates getters for passed in params. No need to set them explicitly. For immutable params use val, for mutable use var 
abstract class FR(var externalId:String, val code:String, val message:String) extends Serializable { 
//need to use override annotation for superclass methods 
override def toString:String={ 
    return "FRworks"; 
} 
} 
// notice how constructor parameters are passed to the base class when defining the child class. 
class RD extends FR("someID","code","msg") { 
    def printId() = println(externalId) 
} 

val x = new RD 
x.externalId = "new ID" //works because externalId is var (mutable) 
x.code = "new code" //error because code is val (immutable) 

x.printId //correctly prints the external id: someID