簡體   English   中英

從Java調用Scala“ val函數”會出錯

[英]Call a Scala “val function” from Java gives error

我有一個scala val函數,如下所示:

  val getTimestampEpochMillis = (year:Int, month:Int, day:Int, hour:Int, quarterHour:Int, minute:Int, seconds:Int) => {
    var tMinutes = minute
    var tSeconds = seconds
    if(minute == 0){
      if(quarterHour == 1){
        tMinutes = 22
        tSeconds = 30
      }else if(quarterHour == 2){
        tMinutes = 37
        tSeconds = 30
      }else if(quarterHour == 3){
        tMinutes = 52
        tSeconds = 30
      }else if(quarterHour == 0){
        tMinutes = 7
        tSeconds = 30
      }
    }

    val localDateTime = LocalDateTime.of(year, month, day, hour, tMinutes, tSeconds)
    localDateTime.atZone(ZoneId.of("GMT")).toInstant().toEpochMilli()
  }

當我從Java調用此函數時,出現以下錯誤:

[ERROR]   required: no arguments                                          
[ERROR]   found: int,int,int,int,int,int,int                              
[ERROR]   reason: actual and formal argument lists differ in length    

調用scala val函數的Java代碼:

Long minTimestampOfGranularity = getTimestampEpochMillis(year, 1, 1, 0, 0, 0, 0);

嘗試

getTimestampEpochMillis().apply(year, 1, 1, 0, 0, 0, 0);

並注意括號()getTimestampEpochMillis() 使用javap檢查生成的類,我們得到

public scala.Function7<java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, scala.runtime.BoxedUnit> getTimestampEpochMillis();

我們在其中看到getTimestampEpochMillis()返回scala.Function7 ,因此在apply參數之前,我們首先必須調用getTimestampEpochMillis()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM