您的位置:首页 > 移动开发 > Android开发

Hello World With Kotlin

2017-05-25 11:33 274 查看
Kotlin 和 Java 一样,可以在PC上运行,可以用IDEA进行开发,环境配置参考Getting Started with IntelliJ IDEA

官方提供了一个学习网站,可以运行一些例子Kotlin

也可以通过命令行的方式编译运行,参考Working with the Command Line Compiler

PC

fun main(args: Array<String>) {
println("Hello, World!")
}


如上,是一个最简单的 Hello World。

Android

package com.hello.test

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.text = "Hello World!" // same ass textView.setText()
}
}


入门

Kotlin代码文件以.kt后缀,源代码所在的目录结构不必与包结构保持一致,但最好和Java一样,目录和包名一致。

Kotlin兼容Java,可以直接调用Java代码;

与Java相比,行结束不需要分号;

包定义和import语句跟Java一样;

变量的定义,” 变量: 类型 “ 的形式,如 savedInstanceState: Bundle?(?表示变量可为null);

函数定义以 fun 开头, 如 fun test(arg: String): Int;

类定义以 class开头,如 class MainActivity : AppCompatActivity()。

*

参考*

Getting Started with IntelliJ IDEA

Working with the Command Line Compiler
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  kotlin android