Skip to content

Instantly share code, notes, and snippets.

@namkyu
Last active May 23, 2018 08:28
Show Gist options
  • Save namkyu/d6a1076fb7ace8903bff081baca56a4c to your computer and use it in GitHub Desktop.
Save namkyu/d6a1076fb7ace8903bff081baca56a4c to your computer and use it in GitHub Desktop.
kotlin test #kotlin
@AnnoClass("nklee")
class AnnoClassImple {
}
// enum
enum class Direction() {
NORTH, SOUTH, WEST, EAST
}
fun greet(name: String) : Unit {
println("Hello, $name!")
}
fun foo(arr: Array<Int>){
println(arr.get(1))
}
fun bar(vararg args: String) {
println(args.get(1))
}
fun main(args: Array<String>) {
val temp: Int = 10
println(temp)
var temp2 = 15
println(temp2)
greet("nklee")
var words : Array<String> = arrayOf("a", "b", "c")
println(words.get(1))
var intArr : Array<Int> = arrayOf(1, 2, 3, 4, 5)
foo(intArr)
var stringArr: Array<String> = arrayOf("a", "b", "c", "d")
bar(*stringArr) // 가변인자에 배열을 전달하는 경우에만 스프레드 연산자(*)를 사용한다.
// 자료형 변환 (as 연산자)
var num : Number = 0
var foo1 : Int = num as Int
// 범위
val myRange : IntRange = 0..10
for (i in myRange) {
// do something
}
val check : Boolean = 5 in myRange // 범위 내에 특정 항목이 있는지 알아보려면
}
println("------------------------------------------------")
println("## 리스트")
println("------------------------------------------------")
// 자료를 수정할 수 없는 리스트 생성
val immutableList : List<String> = listOf("a", "b", "c")
println(immutableList.get(1))
// 자료를 수정할 수 있는 리스트 생성
val mutableList : MutableList<String> = arrayListOf("a", "b")
mutableList.add("c")
// 자료를 수성하지 않는 자료형으로 재할당
val immutableList2 : List<String> = mutableList
println("------------------------------------------------")
println("## 맵")
println("------------------------------------------------")
val immutableMap : Map<String, Int> = mapOf(Pair("A", 65), Pair("B", 66))
val code = immutableMap["A"]
println(code)
val map : Map<String, Int> = mapOf("A" to 65, "B" to 66)
println(map["B"])
// 명명된 인자
drawCircle(x = 10, y = 5, radius = 25)
drawCircle(10, 5, radius = 25)
drawCircle2(10, 5)
drawCircle2(10, 5, 10)
// 명명된 인자
fun drawCircle(x: Int, y: Int, radius: Int) {
}
// 함수의 매개변수에 기본값을 지정할 수 있으며, 이때 지정하는 값을 기본 매개변수라 부른다.
fun drawCircle2(x: Int, y: Int, radius: Int = 25) {
}
// open 키워드를 사용해야 해당 클래스를 상속 받을 수 있다.
open class OpenClass {
// 상속한 클래스에서 재정의할 수 있음
open val openProperty = "foo"
// 상속한 클래스에서 재정의 불가
val finalProperty = "bar"
// 상속한 클래스에서 재정의 가능
open fun openFunc() {}
// 상속한 클래스에서 재정의 불가
fun finalFunc() {}
}
// OpenClass 상속
class FinalClass : OpenClass() {
override val openProperty = "FOO"
override fun openFunc() {
}
}
// 오브젝트를 싱글톤으로 생성하는 방법
object Singleton {
val FOO = "foo"
fun foo() {}
}
println("------------------------------------------------")
println("## 싱글톤")
println("------------------------------------------------")
val fooValue = Singleton.FOO
println(fooValue)
Singleton.foo()
// 중첩 클래스
class Outer {
// 키워드가 없으면 정적 중첩 클래스로 간주
class StaticNested {
}
// 비 정적 중첩 클래스 선언
inner class NonStaticNested {
}
}
println("------------------------------------------------")
println("## 중첩 클래스")
println("------------------------------------------------")
val staticInstance = Outer.StaticNested() // 인스턴스 생성 없이 인스턴스 생성 가능
val nonStaticInstance = Outer().NonStaticNested() // 인스턴스를 생성해야 인스턴스 생성 가능
class Foo(a: Int, b: Int) { // 주 생성자
// 접근 제한자가 없으면 public 으로 간주
val a = 1
protected val b = 2
private val c = 3
internal val d = 4
// 기본 생성자
init {
println("$a $b")
}
// a의 값만 인자로 받는 추가 생성자
constructor(a: Int) : this(a, 0)
// 두 인자의 값을 모두 0으로 지정하는 생성자
constructor() : this(0, 0)
}
abstract class BasicService {
abstract fun bar()
}
interface Bar {
fun baz()
}
println("------------------------------------------------")
println("## 클래스, 인터페이스")
println("------------------------------------------------")
val foo: Foo = Foo(11, 0) // new 키워드 생략
// 추상 클래스의 인스턴스 생성
val basicService = object: BasicService() {
override fun bar() {
}
}
// 인터페이스의 인스턴스 생성
val bar = object : Bar {
override fun baz() {
}
}
// 인자 없는 생성자 호출
Foo()
println("------------------------------------------------")
println("## 확장 함수")
println("------------------------------------------------")
val fooExternal = "Foo"
val foobar = fooExternal.withBar()
println(foobar)
// 확장 함수
// String 클래스에 withPostfix() 함수를 추가
// this 를 사용하여 인스턴스에 접근할 수 있다.
private fun String.withPostfix(postFix : String) = "$this$postFix"
fun String.withBar() = this.withPostfix("Bar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment