2017-07-25 95 views
-2

我有一個方法:我可以在Kotlin中的字符串模板中調用方法嗎?

fun sum(first:Int, second:Int):Int 
{ 
    return first + second 
} 

我可以調用此方法與參數字符串模板中像我可以用一個變量呢?

我試過以下,但它沒有工作:

println("$sum(3,4)") 
+3

爲什麼你不試試? –

+0

加法@OP:如果您問這是因爲您無法訪問Kotlin環境,則可以使用[Ideone](http://www.ideone.com)等網站。 – Moira

+1

我試過這個''println(「$ sum(3,4)」)'' –

回答

4

是,string templates可以包含任意表達式,你只需要使用大括號。

fun foo() = 42 
val bar = 25 

"$bar" 
"${bar}" 
"${foo()}" 
"${2 + 10/5}" 
相關問題