官网:

http://kotlinlang.org/

http://kotlinlang.org/docs/reference/

中文教程: http://kotlindoc.com/

Gradle: http://gradle.org/

KotlinMvc:  http://code.taobao.org/svn/MyKotlinMvc/

安装:

1. Intellij Idea :

2. Kotlin 编译器。 https://github.com/JetBrains/kotlin

JVM最简生存指南

http://www.importnew.com/10127.html

必读, 是写给 .Net 开发者 转 Java 人员的。

Spring+Kotlin:

https://spring.io/blog/2016/02/15/developing-spring-boot-applications-with-kotlin

技巧:

1. 代码混编。使用 Intellij Idea ,时,可以使用 Java + Kotlin + Scala 混合编译。 需要添加各自的包引用。

2. 把Java代码 Copy 到 Kotlin 时,会有提示,让系统自动进行转换。

3. 函数的参数是不可修改的, 如果想修改, 可以重新定义一个同名的参数,如:

var startIndex = startIndex;

问题

1. 泛型

  Java声明泛型时,在使用时,可以不指定泛型, 如: List jm = new ArrayList<String>();   C#可以这样:

  public class JsonMsg<T> : JsonMsg{}

  但 Kotlin 不能使用同名泛型类。

2.  关于 Maven :

  http://blog.csdn.net/ubuntu64fan/article/details/7462981 ,作者极力反对, 首推: Rake , 其次是 Ant

  http://westsky.blog.51cto.com/358372/1590573 ,推荐顺序是: Gradle > Ant > Maven.

3.  Cloneable

  Cloneable 接口的 clone 方法居然是 protected,直接导致泛型参数是Cloneable对象无法直接调用 clone 方法。

4 . 死循环调用:

open class MapModel2   : HashMap<String,String> ()  {
companion object{} override fun get(key: String): String {
return super.get(key).toString()
}
}
class abc : MapModel2() {
companion object{
@JvmStatic
var instance:MapModel2? = null;
}
var Name= ""; override fun get(key: String): String {
return super.get(key).toString()
}
}
fun main(arg: Array<String>) {
abc.instance = abc();
abc.instance!!["Name"] = "OK";
println(abc.instance!!["Name"]);
}

必须逐级重写 get 方法,否则就会: 在 abc 里执行get , 在 MapModel2 里执行get ,由于没有重写,转而执行 abc 里的 get 继而造成死循环。

该问题已提交到jetbrains ,并已得到解决: https://youtrack.jetbrains.com/issue/KT-13116?replyTo=27-1522462 。

5. jvm-target 1.6的问题

Bug1:

Kotlin Jar包项目,使用 Artifacts Build,是不对的,会使用 1.6

  查看 jar包里的 /META-INF/MANIFEST.MF 内容如下:

  

Manifest-Version: 1.0
Implementation-Title: HttpComponents Apache HttpClient
Implementation-Version: 4.5.3
Archiver-Version: Plexus Archiver
Built-By: oleg
Specification-Vendor: The Apache Software Foundation
Implementation-Vendor-Id: org.apache
Specification-Title: HttpComponents Apache HttpClient
url: http://hc.apache.org/httpcomponents-client
Implementation-Vendor: The Apache Software Foundation
X-Compile-Target-JDK: 1.6
Implementation-Build: tags/4.5.3-RC1/httpclient@r1779741; 2017-01-21 1
6:58:35+0100
X-Compile-Source-JDK: 1.6
Created-By: Apache Maven 3.0.5
Build-Jdk: 1.7.0_75
Specification-Version: 4.5.3

Bug2:

  在 idea 的 Project Structure 里,把 target platform 改为 1.6 ,是不成功的。 除非选中 使用项目设置(use project settings )再选择 1.8

查看项目的 iml 文件。

 <facet type="kotlin-language" name="Kotlin">
<configuration version="2" platform="JVM 1.6" useProjectSettings="false">
<compilerSettings />
<compilerArguments>
<option name="jvmTarget" value="1.6" />
<option name="languageVersion" value="1.1" />
<option name="apiVersion" value="1.1" />
<option name="pluginClasspaths">
<array />
</option>
<option name="coroutinesWarn" value="true" />
<option name="pluginOptions">
<array />
</option>
</compilerArguments>
</configuration>
</facet>

应该使用 Maven 的 Package 打包,如果出现: 1.8 1.6 的错误,应该添加  jvmtarget 如下:

<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<jvmTarget>1.8</jvmTarget>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration> <executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>

jar包项目和引用jar包的项目都要添加。

编译运行的差异

编译: mvn clean kotlin:compile package

运行  jar -jar  -noverify  xx.jar

字节码校验器 , 具体校验:
变量要在使用之前进行初始化。
方法调用与对象引用类型之间要匹配。
访问私有数据和方法的规则没有被违反。
对本地变量的访问都在运行时堆栈内。
运行时堆栈没有溢出。

java 可以使用  -noverify

tomcat Java_opts 在  setenv.sh 里,分两种情况:

1. startup.sh , 可以用 -noverify

2. daemon.sh 必须用 -Xverify:none

-Xverify:none 兼容性最好.

快捷键:

使用 VS 设置。

Ctrl + Shift + C 拷贝当前文件的全路径。

计算属性生成Json字段

如果设置了 JSON 序列化字段属性,Kotlin 中的 val 计算属性 不会生成字段,所以不会生成 JSON字段。

典型的例子是  IdUrl 中添加  val fullUrl 字段,该字段是根据 host 配置 以及 Url ,动态返回的属性.

添加 @JvmField 会报错,因为不能作用于 val 属性的 get() 方法。

解决方法:

class IdUrl {
fun set_fullUrl() {
if (this.url.isEmpty()) return;
this.fullUrl = "http://dev8.cn" + this.url;
} var fullUrl: String = ""
get
       private set var url: String = ""
get() = field
set(value) {
field = value;
this.set_fullUrl()
} }

Kotlin笔记的更多相关文章

  1. Kotlin笔记小结(For Java Developer)

    这篇文章为kotlin学习记录,主要针对的是自己的知识盲区,不适用于新手. 文中所有demo均来自于kotlin官网 类型 整形 Type Size (bits) Min value Max valu ...

  2. kotlin 冷知识 *号 展开数组

    Kotlin笔记-冷门知识点星号(*) 2019年05月10日 11:37:00 weixin_33724059 阅读数 6   可变参数展开操作符 在数组对象前加*号可以将数组展开,方便传值,比如: ...

  3. Kotlin Tutorials系列文章

    Kotlin Tutorials系列文章 想写一个有实用价值的Kotlin笔记, 让一线开发一看就懂, 看完就能上手. 当然官方文档更有参考价值了. 这个系列相对于官方文档的大而全来说, 最主要优势是 ...

  4. 开源分享:谷歌大佬联合打造《高级Kotlin强化实战(附Demo)》

    Kotlin 以其简洁的特性而闻名,而在我们的实践中,更加简洁就意味着更加高效.事实上,在使用 Kotlin 的专业 Android 开发者中,有多达 67% 的人表示 Kotlin 已经帮助他们提升 ...

  5. Kotlin入门学习笔记

    前言 本文适合人群 有一定的java基础 变量与方法 变量声明及赋值 var 变量名: 变量类型 val 变量名: 变量类型 这里,var表示可以改变的变量,val则是不可改变的变量(第一个赋值之后, ...

  6. Kotlin 学习笔记(一)

    (Kotlin 学习笔记的文档结构基本按照 Java 核心技术一书的目录排列) 基本程序设计结构 数据类型 数字 类型 宽度 Double 64 Float 32 Long 64 Int 32 Sho ...

  7. 08_28学习笔记Kotlin

    08_28学习笔记Kotlin Kotlin语法 aoe : int=18: name : String ="name"; 函数的定义 fun 名称 (str:String) :S ...

  8. Kotlin学习笔记(9)- 数据类

    系列文章全部为本人的学习笔记,若有任何不妥之处,随时欢迎拍砖指正.如果你觉得我的文章对你有用,欢迎关注我,我们一起学习进步! Kotlin学习笔记(1)- 环境配置 Kotlin学习笔记(2)- 空安 ...

  9. Kotlin for Java Developers 学习笔记

    Kotlin for Java Developers 学习笔记 ★ Coursera 课程 Kotlin for Java Developers(由 JetBrains 提供)的学习笔记 " ...

随机推荐

  1. Python 爬取网站资源文件

    爬虫原理: 以下来自知乎解释 首先你要明白爬虫怎样工作.想象你是一只蜘蛛,现在你被放到了互联“网”上.那么,你需要把所有的网页都看一遍.怎么办呢?没问题呀,你就随便从某个地方开始,比如说人民日报的首页 ...

  2. Range Sum Query 2D - Immutable

    https://leetcode.com/problems/range-sum-query-2d-immutable/ 条件说sumRegion 会调很多次,如果每次都用双for 循环去累加的话就有太 ...

  3. Android-Menu [使用C# And Java实现]

    本篇是对安卓菜单使用编程方式实现,当然可以使用XML的方式完成同样的功能,基本Java和C#写法都是一致的,所以使用XML的方式在本篇中使用Java演示,需要注意的是,对于如果不是VS开发的话,那么资 ...

  4. 关于 js 一些基本的东西

    r.js 可以打包(可以实现前端文件的压缩与合并). 客户端尽量遵循 amd 规范. 推荐使用 requirejs 规范. requirejs 简单教程: http://www.runoob.com/ ...

  5. 20145223《信息安全系统设计基础》 GDB调试汇编堆栈过程分析

    20145223<信息安全系统设计基础> GDB调试汇编堆栈过程分析 分析的c语言源码 生成汇编代码--命令:gcc -g example.c -o example -m32 进入gdb调 ...

  6. 2016 ACM/ICPC Asia Regional Dalian Online 1008 Function 二分+RMQ

    Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  7. Delphi 中的自动释放策略-转

    八.使用结构体而不是结构体指针: 很重要 一.指定 Owner 后, 随 Owner 连带释放: //uses Vcl.StdCtrls, Vcl.ExtCtrls; var panel: TPane ...

  8. [译]App Framework 2.1 (2)之 Get Involved

    App Framework  API 第二篇 原文在此:http://app-framework-software.intel.com/documentation.php#intro/involved ...

  9. 关于H5填写信息类页面横向布局总结

    接触h5已经有快一年了,因为一直偏向页面重构所以在页面布局方面也算是经历过风风雨雨.所以总结一下自己用过的方法来比较归纳 首先来说,H5的页面一般分为两种,一种是用来做市场营销的,主要特征是图多,页面 ...

  10. js中创建对象的三种方式

    1. 对象字面量 var obj={ name:"小小", age:3, car:{ brand:"baoma", } }; } 2.使用内置构造函数 var ...