목록콤퓨타 왕기초 (187)
파게로그
Data Class 오직 데이터를 저장하기 위해서 클래스를 생성하는 경우가 있다. VO나 DTO가 대표적인 예시이다. 이러한 경우에, 데이터 클래스를 생성하기 위해 data 키워드로 data class임을 표현할 수 있다. data class에 대해서 컴파일러는 자동으로 다음과 같은 것들을 생성해준다. ▪ copy() function ▪ equals(), hashCode() pair ▪ toString() form of the primary constructor ▪ componentN() functions Data Class이기 위한 요구사항 ▪ primary constructor는 최소한 한 개의 파라미터를 가진다. ▪ primary constructor의 파라미터들은 val 또는 var이 표시되어야 ..
Java와 같이, Kotlin은 nested class라고 알려진, 한 클래스 내부에 다른 클래스를 정의하는 것이 가능하다. 내부 클래스에의 접근은 다른 멤버들과 같이 .을 통해서 가능하다. 그런데 Java에서는 어떤 클래스 내부에 다른 클래스를 선언하면 이는 기본적으로 inner class가 되지만, Kotlin에서는 inner class를 생성하기 위해서는 inner modifier를 사용해야만 한다. nested class는 outer class에 접근할 수 없다. 예를 들어서 다음 코드는 컴파일 에러가 발생하는데, nested class가 outer class의 property인 foo에 접근을 시도했기 때문이다. class Outer { val foo = "Outer class" class Ne..
Kotlin에서 abstract 키워드는 추상 클래스를 선언하기 위해 사용된다. 추상 클래스는 초기화될 수 없다. 즉 추상 클래스의 객체를 생성하는 것은 불가능하다. 그러나 추상 클래스를 상속받는 것은 가능하다. properties, methods 등, 추상 클래스의 멤버는, abstract 키워드를 사용하지 않는 한 abstract하지 않다. abstract class Person(name: String = "Unknown", age: Int = 0) { var name = name var age = age abstract fun describe() fun introduce() { println("I am $name and $age years old.") } } class Student(name: St..
접근 제한자... 클래스, 객체, 인터페이스, 생성자, 함수, property, setter에 대한 키워드이다. 다만 getter의 visibiltiy는 언제나 property의 그것과 같기 때문에 설정할 수 없다. 그리고 명시되지 않으면 public으로 설정된다. package 내부의 visibility modifiers package는 관련된 함수, properties, 클래스, 객체, 인터페이스의 집합을 구성한다. Modifier Description public declarations are visible everywhere private visible inside the file containing the declaration internal visible inside the same modul..
Inheritance(상속)은 OOP의 핵심적인 기능 중 하나로서, 사용자가 기존에 존재하는 base class로부터 새로운 derived class를 생성할 수 있도록 한다. derived class는 base class로부터 모든 자질을 이어받으며, 자체적인 추가 자질을 가질 수 있다. inheritance를 이용하고자 한다면, derived class가 is a 관계가 맞는지 잘 확인해야만 한다. 기본적으로 Kotlin에서 클래스는 final이기에, open 키워드를 붙여서 이를 통해 derived class를 생성할 수 있도록 해야 한다. open class Person(var name: String, var age: Int) { init { println("Person init: Created ..
https://kotlinlang.org/docs/properties.html https://umbum.dev/591 Getters and Setters 프로그래밍에서, getters는 property의 값을 얻기 위해 사용되며, setters는 property의 값을 설정하기 위해 사용된다. Kotlin에서 getters와 setters는 수의적이며, 이를 직접 생성하지 않을 때에는 자동으로 생성된다. How Getters and Setters Work class Student() { var name: String = "Unknown" } fun main(args: Array) { val student = Student() println(student.name) student.name = "Miu" p..
Constructors 생성자는 클래스 속성을 초기화하는 데 있어서 정확한 방법이다. 생성자는, 특별한 멤버 함수로서, 객체가 처음 생성될 때(instantiated) 호출된다. 그러나 Kotlin에서 작동하는 방법은 다소 다르다. Kotlin에서는 두 가지의 생성자가 있다. ▪ Primary constructor consise way to initialize a class ▪ Secondary constructor allows you to put additional initialization logic Primary Constructor primary constructor는 클래스 헤더의 일부이다. class Person(val firstName: String, var age: Int) { // cla..
Kotlin Introduction to OOP Kotlin은 함수형 프로그래밍과 객체지향형 프로그래밍을 모두 지원한다. higher-order functions, function types, lambdas를 지원하여 함수형 프로그래밍 스타일에서 일하는 좋은 환경을 제공한다. 덩달아 객체지향형 프로그래밍도 지원한다. OOP에서는, 복잡한 문제는 객체를 생성함으로써 작은 문제로 쪼갤 수 있다. 이러한 객체는 두 개의 특성을 공유한다. state와 behavior이다. 예를 들면 다음과 같다. 예1) Lamp는 객체이다. ▪ on 상태와 off 상태가 있다. ▪ turn on 행위와 turn off 행위가 있다. 예2) Bicycle은 객체이다. ▪ current gear, two wheels, number..