목록프로그래밍/코틀린 (18)
불로구
import jdk.nashorn.internal.runtime.JSType.toDouble fun main(array: Array){ var cal:Calculator = Calculator() println(cal.plus(1,2,3,4,5,6,7,8,9,10)) println(cal.minus(10,3,5,1)) println(cal.multiply(1,2,3,4,5,6,7,8,9,10)) println(cal.divided(10,3,2)) } class Calculator(){ fun plus(vararg numbers:Int):Int{ var result: Int = 0 numbers.forEach{ result = result + it } return result } fun minus(vara..
앞에서 코틀린의 클래스생성 방법을 알아보았다. 이번에는 생성한 코틀린 클래스를 활용해보자. 우선 로봇공장 클래스를 하나 생성했다. class RobotFactory{ var color : String = "" var size : String = "" fun start(){ println("로봇 조립을 시작한다.") } fun choiceColor(color:String){ this.color = color println("$color 색으로 설정되었습니다.") } fun choiceSize(size:String){ this.size = size } fun doc(){ if(color.isEmpty() || size.isEmpty()){ println("로봇의 색상 또는 사이즈를 설정해주세요.") }els..
클래스 - 클래스는 객체가 가지고 있는 데이터와 동작방법을 나타내는 정보이다. - 프로그램 설명서에 의해 클래스가 생성되고 메모리에 객체가 만들어지면 인스턴스화 된 것이라 볼 수 있다. 클래스 생성 - 클래스는 class라는 키워드를 사용한다. 우선 간단하게 클래스를 생성해보자 class Computer(var cpu:String, var graphic:String, var ram:Int){ override fun toString() = "cpu : $cpu , graphic : $graphic , ram : $ram" } Computer란 클래스를 생성하며 인자로 cpu, graphic, ram을 받았다. toString을 오버라이딩하여 각가의 값을 출력해주었다. val cpu1 = Computer("..
코틀린을 활용한 여러가지 반복문을 알아보자 package com.example.myapplication.Kotlin fun main(array: Array){ var a = mutableListOf(1,2,3,4,5,6,7,8,9) //반복 1 for(item in a){ if(item == 5){ println("다섯번째") }else { println(item) } } //반복 2 for((index, item) in a.withIndex()){ println("index : $index , value : $item") } // 반복 3 a.forEach{ println(it) } println() //반복 4 a.forEach{ item -> println(item) } //반복 5 a.forEac..