试图了解Kotlin示例(Trying to understand Kotlin Example)
我想学习Kotlin,并通过try.kotlinlang.org上的示例进行工作
我无法理解一些示例,特别是Lazy属性示例: https : //try.kotlinlang.org/#/Examples/Delegated%20properties/Lazy%20property/Lazy%20property.kt
/** * Delegates.lazy() is a function that returns a delegate that implements a lazy property: * the first call to get() executes the lambda expression passed to lazy() as an argument * and remembers the result, subsequent calls to get() simply return the remembered result. * If you want thread safety, use blockingLazy() instead: it guarantees that the values will * be computed only in one thread, and that all threads will see the same value. */ class LazySample { val lazy: String by lazy { println("computed!") "my lazy" } } fun main(args: Array<String>) { val sample = LazySample() println("lazy = ${sample.lazy}") println("lazy = ${sample.lazy}") }
输出:
computed! lazy = my lazy lazy = my lazy
我不知道这里发生了什么。 (可能是因为我不熟悉lambda)
为什么println()只执行一次?
我也对“我懒”字符串没有分配给任何东西(String x =“my lazy”)或返回中使用(返回“我的懒惰”)这一行感到困惑。
有人可以解释吗? :)
I want to learn Kotlin and am working through the Examples on try.kotlinlang.org
I have trouble understanding some examples, particularly the Lazy property example: https://try.kotlinlang.org/#/Examples/Delegated%20properties/Lazy%20property/Lazy%20property.kt
/** * Delegates.lazy() is a function that returns a delegate that implements a lazy property: * the first call to get() executes the lambda expression passed to lazy() as an argument * and remembers the result, subsequent calls to get() simply return the remembered result. * If you want thread safety, use blockingLazy() instead: it guarantees that the values will * be computed only in one thread, and that all threads will see the same value. */ class LazySample { val lazy: String by lazy { println("computed!") "my lazy" } } fun main(args: Array<String>) { val sample = LazySample() println("lazy = ${sample.lazy}") println("lazy = ${sample.lazy}") }
Output:
computed! lazy = my lazy lazy = my lazy
I don't get what is happening here. (probably because I am not really familiar with lambdas)
Why is the println() only executed once?
I am also confused about the line "my lazy" the String isn't assigned to anything (String x = "my lazy") or used in a return (return "my lazy)
Can someone explain please? :)
原文:https://stackoverflow.com/questions/46573364
最满意答案
转到
Project
»Properties
»Java Build Path
»Libraries
并删除除你的android SDK版本以外的所有单击确定。 转到Project
»Clean
»Clean projects selected below
select your project and click OK
»select your project and click OK
我希望这会有所帮助。另一个原因是,如果您的JAR文件位于项目文件夹中的某处,然后将其作为Java Path Library添加,那么它可能是JAR文件冲突。 它不会在包资源管理器下显示,所以您不会注意到它,但它会计算两次,导致可怕的Dalvik错误
Go to
Project
»Properties
»Java Build Path
»Libraries
and remove all except your android SDK version click OK. Go toProject
»Clean
»Clean projects selected below
»select your project and click OK
I hope this will help.Another reason is that it can be a JAR file conflict, if you have a JAR file located somewhere in your project folder and then added it as a Java Path Library. It does not show up under the Package Explorer, so you don't notice it, but it does get counted twice, causing the dreaded Dalvik error
相关问答
更多-
Android导出给出“转换为Dalvik格式失败的错误1”(Android export give a “Conversion to Dalvik format failed error1”)[2022-09-06]
在“项目菜单”上禁用“自动构建”并手动重建(右键单击项目 - >构建项目),然后尝试重新导出应用程序。 Eclipse似乎与导出和编译同时发生冲突。 所以每次你需要导出一个APK,禁用自动构建。 任何时候使用它都是安全的。 Disable "Build Automatically" on "Project Menu" and rebuild it manually (Right click on project -> Build project), then try to export the applic ... -
外部JAR上“转换为Dalvik格式失败,错误1”(“Conversion to Dalvik format failed with error 1” on external JAR)[2022-06-05]
转到项目»属性»Java构建路径»库并删除除“Android XY”之外的所有内容(在我的例子中为Android 1.5)。 单击确定。 转到项目»清洁»清洁下面选择的项目»选择您的项目,然后单击确定。 这应该工作。 您也可以将JAR文件放置在项目文件夹的某处(我将Admob JAR文件复制到我的src文件夹中),然后将其添加为Java路径库。 它不会显示在包资源管理器下,所以你不会注意到它,但它确实被计数了两次,导致可怕的Dalvik错误1。 另一个可能的原因可能是包名冲突。 假设你有一个包com.abc ... -
如何解决问题转换为Dalvik格式失败,错误1?(how can resolve the issue Conversion to Dalvik format failed with error 1?)[2023-08-23]
转到Project » Properties » Java Build Path » Libraries并删除除你的android SDK版本以外的所有单击确定。 转到Project » Clean » Clean projects selected below select your project and click OK » select your project and click OK我希望这会有所帮助。 另一个原因是,如果您的JAR文件位于项目文件夹中的某处,然后将其作为Java Path Li ... -
我最近在eclipse中遇到了同样的问题。 发生这种情况是因为您的项目包含太多类,包括所有库类。 我已经通过使用proguard来压缩构建文件来解决这个问题。 这非常有用,因为它可以通过删除未使用的代码来自动缩小,优化和混淆代码。 在这里了解计划: http://developer.android.com/tools/help/proguard.html *记住预备会发出太多警告,必须小心使用。 要么 你也可以使用链接: https://www.contentful.com/blog/2014/10/30/ ...
-
转换为Dalvik格式失败,错误2 adt 16.0.1(Conversion to Dalvik format failed with error 2 adt 16.0.1)[2022-03-28]
我发现这是一个老问题,但我找到了另一个解决方案: 从eclipse.ini 删除 -XX:+AggresiveOpts 。 Actually it's becouse my lib projects pacage name is not the same as in my main project. -
在编辑我的屏幕截图时,我发现可能会导致此问题。 正如你所见,我有两个android-support-v4.jar 。 我已经删除了android-support-v4.jar,干净的FacebookSDK项目和我的my-app-phone项目正确启动。 [2013-03-16 18:37:23 - my-app-phone] ------------------------------ [2013-03-16 18:37:23 - my-app-phone] Android Launch! [2013-03 ...
-
我如何修复错误“转换为dalvik格式失败,错误1”?(how do i fix the error “conversion to dalvik format failed with error 1”?)[2023-05-14]
尝试在proguard.cfg中添加-forceprocessing ! 这是唯一适合我的事情(清理项目或其他任何事情都没有)。 Proguard文档告诉: 指定处理输入,即使输出似乎是最新的。 最新性测试基于指定的输入,输出和配置文件或目录的日期戳的比较。 那个选项没有错! I found the answer apparently it's something that broke back in r12, showed up when I updated, thus prompting this qu ... -
问题解决了 ! 我已经包含了两个GestureUtils.class的定义,因为我使用了与android sdk发生冲突的隐藏包framework.jar。 Problem solved ! I have included two definitions of GestureUtils.class because I have used the hidden pack framework.jar which has this conflict with android sdk ..
-
问题终于解决了,使用这个来自惊人的@TheTerribleSwiftTomato的解决方案 。 这都是由“ Android SDK工具中的库项目的更改,r14 ”引起的。 感谢@Google让我的生活变得如此悲惨。 如果你对系统的行为进行了彻底的改变,那么当我还没准备好吞噬它附带的所有信息时(或者至少发出一个错误),不要强迫我从SDK 9“升级”到SDK 16可以直接指向此更改的消息)。 另一方面,真正感谢@Google引入了期待已久,热切期待的功能,该功能可将每个库项目生成到自己的JAR文件中 。 这是一 ...
-
这是由于罐子里的冲突造成的。 尝试重新导入并将project.properties更改为此代码。 第1步:清理项目 步骤2.取出所有罐子 第3步。编辑project.properties 第4步。刷新项目。 第5步。运行 - 导出已签名的apk 这是代码。 -injars bin/classes -injars libs -outjars bin/classes-processed.jar -dontpreverify -repackageclasses '' -allowac ...