zoukankan      html  css  js  c++  java
  • Kotlin笔记

    官网:

    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() } }
  • 相关阅读:
    Hibernate笔记
    Struts2笔记(学struts2只需要这一篇文章)
    Linux开机启动和登录时各个文件的执行顺序
    Java虚拟机之垃圾回收算法思想总结
    码农雷林鹏:php概述
    码农雷林鹏:php教程
    雷林鹏分享:JDBC驱动类型
    码农雷林鹏教程分享:JDBC实例代码
    雷林鹏分享:JDBC环境设置
    雷林鹏分享:JDBC SQL语法
  • 原文地址:https://www.cnblogs.com/newsea/p/5515407.html
Copyright © 2011-2022 走看看