<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>kotlin笔记 - 南极滑稽的博客</title>
	<atom:link href="https://blog.nanjihuaji.top/category/programming/kotlin%e7%ac%94%e8%ae%b0/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.nanjihuaji.top</link>
	<description>一个学术垃圾的博客</description>
	<lastBuildDate>Mon, 19 Aug 2024 10:26:04 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.nanjihuaji.top/wp-content/uploads/2024/08/cropped-Cache_45925d60c206ddd8-150x150.jpg</url>
	<title>kotlin笔记 - 南极滑稽的博客</title>
	<link>https://blog.nanjihuaji.top</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Kotlin学习笔记（1）</title>
		<link>https://blog.nanjihuaji.top/2024/08/19/kotlin%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%881%ef%bc%89/</link>
					<comments>https://blog.nanjihuaji.top/2024/08/19/kotlin%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%881%ef%bc%89/#respond</comments>
		
		<dc:creator><![CDATA[Nanjihuaji]]></dc:creator>
		<pubDate>Mon, 19 Aug 2024 10:26:04 +0000</pubDate>
				<category><![CDATA[kotlin笔记]]></category>
		<category><![CDATA[编程]]></category>
		<guid isPermaLink="false">https://blog.nanjihuaji.top/?p=287</guid>

					<description><![CDATA[<p>变量 变量声明 kotlin使用val或var声明变量。 val用于声明一个不可变变量，它指向的对象本身可以更 [&#8230;]</p>
<p>The post <a href="https://blog.nanjihuaji.top/2024/08/19/kotlin%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%881%ef%bc%89/">Kotlin学习笔记（1）</a> first appeared on <a href="https://blog.nanjihuaji.top">南极滑稽的博客</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1>变量</h1>
<h2>变量声明</h2>
<p>kotlin使用<code>val</code>或<code>var</code>声明变量。</p>
<p><code>val</code>用于声明一个<strong>不可变</strong>变量，它指向的对象本身可以更改而它指向的对象是谁不能更改，这表明之后不能再给它重新赋值。</p>
<p><code>var</code>用于声明一个<strong>可变</strong>变量，它指向的对象本身与它指向的对象是谁都可以被修改，这表明之后可以给它重新赋值。</p>
<p>声明的变量类型可以被自动推断，也可以手动指定，例如：</p>
<pre><code class="language-kotlin">val x = 5 // 自动推断类型为Int
val y : Byte = 5 // 手动指定类型为Byte</code></pre>
<h2>数字</h2>
<p>就整数而言，kotlin有这些整数类型：</p>
<table>
<thead>
<tr>
<th>变量类型</th>
<th>大小/bit</th>
<th>最小</th>
<th>最大</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Byte</code></td>
<td>8</td>
<td>-128</td>
<td>127</td>
</tr>
<tr>
<td><code>Short</code></td>
<td>16</td>
<td>-32768</td>
<td>32767</td>
</tr>
<tr>
<td><code>Int</code></td>
<td>32</td>
<td>-2^{31}</td>
<td>2^{31} - 1</td>
</tr>
<tr>
<td><code>Long</code></td>
<td>64</td>
<td>-2^{63}</td>
<td>2^{63} - 1</td>
</tr>
</tbody>
</table>
<p>定义变量时，可以在数字后添加后缀<code>L</code>指定这个变量为<code>Long</code>，如果数字足够大，也会定义为<code>Long</code>。</p>
<pre><code class="language-kotlin">val a : Long = 100
val a = 100L</code></pre>
<p>kotlin还提供<code>float</code>和<code>double</code>两种浮点数。<code>float</code>为32比特，而<code>double</code>为64比特。</p>
<p>在定义浮点数时，默认定义为<code>double</code>。可以在数字后添加后缀<code>f</code>指定其为浮点数。</p>
<p>一个较小的数字类型在赋值时<strong>不能隐式转换</strong>为较大的数字类型。例如，下面的代码是非法的：</p>
<pre><code class="language-kotlin">val a = 100
val b : Long = a // a不会被隐式转化为Long</code></pre>
<p>需要采用显式类型转换。显式类型转换为数字的toXXX()方法。</p>
<pre><code class="language-kotlin">val a = 100
val b : Long = a.toLong()</code></pre>
<p>在进行运算的时候，较小的数字类型会被隐式的转化为较大的数字类型。</p>
<h1>函数</h1>
<h2>函数声明</h2>
<p>一个函数由如下的结构声明：</p>
<pre><code class="language-kotlin">fun functionname(arg1: Type, arg2: Type, ..., argn: Type): ReturnType {
    // some codes
}</code></pre>
<p>返回值类型不填默认为<code>Unit</code>，表示无意义返回值。</p>
<h2>程序入口</h2>
<p>一个kotlin程序由<code>main()</code>函数开始执行: </p>
<pre><code class="language-kotlin">fun main() {
    // some codes
}</code></pre>
<h2>不定长参数</h2>
<p><code>vararg</code>关键字用于定义不定长的参数。</p>
<pre><code class="language-kotlin">fun myAverage(vararg numbers: Double): Double {
    var sum = 0.0
    for (number in numbers) {
        sum += number
    }
    return sum / numbers.size
}</code></pre>
<h2>匿名函数</h2>
<p>匿名函数由下定义：</p>
<pre><code class="language-kotlin">val func: (Type, Type) -> (ReturnType) = {arg1, arg2 -> ReturnValue}</code></pre>
<h2>高阶函数</h2>
<p>函数的参数或返回值可以是一个函数。</p>
<h3>函数作为参数</h3>
<p>一个函数作为参数被传入时，需要在其前面加<code>::</code>表示传入的函数做参数。</p>
<pre><code class="language-kotlin">val a = listOf(1, 2, 3)
a.forEach(::println) // 这会换行输出1 2 3</code></pre>
<h3>函数作为返回值</h3>
<p>一个函数作为返回值时，这个函数应该这样定义：</p>
<pre><code class="language-kotlin">fun fun1(fun1&#039;s args): (fun2&#039;s args) -> fun2的返回值 {
    // some codes
}</code></pre>
<p>例如：</p>
<pre><code class="language-kotlin">fun returnFun(): (Int, Int) -> Int {
    return {1, j -> (i + j)} // 这是一个匿名函数
}</code></pre>
<h3>闭包函数</h3>
<p>闭包(closure)就是能够读取其他函数内部变量的函数。</p>
<pre><code class="language-kotlin">fun counter(): () -> Int {
    var count = 0
    return {
        count++
        count
    }
}

val c = counter()
println(c()) // 1
println(c()) // 2
println(c()) // 3</code></pre><p>The post <a href="https://blog.nanjihuaji.top/2024/08/19/kotlin%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%881%ef%bc%89/">Kotlin学习笔记（1）</a> first appeared on <a href="https://blog.nanjihuaji.top">南极滑稽的博客</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.nanjihuaji.top/2024/08/19/kotlin%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%881%ef%bc%89/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
