2017-07-25 92 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}" 
相关问题