markdown [Kotlin基本语法] #kotlin

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown [Kotlin基本语法] #kotlin相关的知识,希望对你有一定的参考价值。

### 1. Kotlin primary header constructor
```Kotlin
// the immutable parameters passed to the constructor are not member variables!, they need to be explicitly created as below
// parent class takes in the passed values to the child class.
class MyListAdapter(ctx: Context, res: Int) : ArrayAdapter<String>(ctx, res)
	val context: Context
    val resource: Int
    
    init {
    	context = ctx
        resource = res
    }
    ...
```

### 2. Define a sample data array
Arrays in Kotlin are represented by the Array class, that has get and set functions (that turn into [] by operator overloading conventions), and size property, along with a few other useful member functions.
To create an array, we can use a library function `arrayOf()` and pass the item values to it, so that `arrayOf(1, 2, 3)` creates an array [1, 2, 3]. Alternatively, the arrayOfNulls() library function can be used to create an array of a given size filled with null elements.
```Kotlin
val num_items = arroyOf(1, 2, 3)
val str_items = arrayOf("hello", "world")
val any_items = arrayOf("Hello", 3, "h") // array of type Any

// get item
num_items.get(1)
num_item[1]

// set item
str_items.set(1, "new")
str_items[1] = "new"
```

以上是关于markdown [Kotlin基本语法] #kotlin的主要内容,如果未能解决你的问题,请参考以下文章

快速上手 Kotlin 的 11 招

浅谈Kotlin:基本类型基本语法代码风格

[译]20个学习Kotlin的优质资源

一个类让你了解 kotlin 最基本的语法

Kotlin VS Java:基本语法差异(转载)

Kotlin基本语法和使用