SlideShare une entreprise Scribd logo
1  sur  72
Télécharger pour lire hors ligne
Kotlin Class
LazySoul
우명인
Class
/* Java */
public class PersonJava {
private final String name;
public PersonJava(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Class
/* Java */
Person person = new Person("myeonginWoo");
System.out.println(person.getName());
Class
/* Kotlin */
class Person(val name: String)
Class
/* Kotlin */
val person = Person("myeonginWoo")
println(person.name)
Class
/* Kotlin */
val person = Person("myeonginWoo")
println(person.name)
New 연산자 필요 없음.
getName() 이 아님
Property
class Person(firstName: String, lastName: String, age: Int)
class Person2(val firstName: String, val lastName: String, val age: Int)
Property
class Person(firstName: String, lastName: String, age: Int)
class Person2(val firstName: String, val lastName: String, val age: Int)
val person = Person("myeongin", "woo", 32 )
val person2 = Person2("myeongin", "woo", 32 )
Property
class Person(firstName: String, lastName: String, age: Int)
class Person2(val firstName: String, val lastName: String, val age: Int)
val person = Person("myeongin", "woo", 32 )
val person2 = Person2("myeongin", "woo", 32 )
println(person.firstName) // Error
println(person2.firstName) // Ok
Property
class Person(firstName: String, lastName: String, age: Int)
class Person2(val firstName: String, val lastName: String, val age: Int)
• Java의 class 내부에 선언되는 field와 비슷하지만 다릅니다.
• field는 별도로 getter, setter를 선언해줘야 합니다.
• propertry는 getter와 setter를 자동으로 생성해 줍니다.
• public, private, protected 와 같은 접근제어자만 잘 사용한다면 보일러플레이트들이 많
이 제거되어 실제 로직에 더 집중 할 수 있습니다.
Property
/* Java */
public class PersonJava {
String firstNam;
String lastName;
int age;
public PersonJava(String firstName, String lastName, int age) {
this.firstNam = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstNam() {
return firstNam;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public void setFirstNam(String firstNam){
this.firstNam = firstNam;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAge(int age){
this.age = age;
}
Property
/* Java */
public class PersonJava {
String firstNam;
String lastName;
int age;
public PersonJava(String firstName, String lastName, int age) {
this.firstNam = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstNam() {
return firstNam;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public void setFirstNam(String firstNam){
this.firstNam = firstNam;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAge(int age){
this.age = age;
}
/* kotlin */
class PersonKotlin(
var firstName: String,
var lastName: String,
var age: Int)
Property
class Person {
var name: String = ""
get() = field.toUpperCase()
set(value) {
field = "Name : " + value
}
}
Property
class Person {
var name: String = ""
get() = field.toUpperCase()
set(value) {
field = "Name : " + value
}
}
person.name = "test"
Property
class Person {
var name: String = ""
get() = field.toUpperCase()
set(value) {
field = "Name : " + value
}
}
person.name = "test"
println(person.name) //NAME : TEST
Constructor
class Person(firstName: String, lastName: String, age: Int)
class Person constructor(firstName: String, lastName: String, age: Int)
val person = Person(“myeongin”, “woo”, 32)
Constructor
class Person(firstName: String, lastName: String, age: Int)
class Person constructor(firstName: String, lastName: String, age: Int)
val person = Person(“myeongin”, “woo”, 32)
class 이름 뒤에 constructor을 붙이는 것을 primary constructor 이라고 합니다.
기본적으로 생략해도 되지만 어노테이션을 붙여야 할 필요가 있을 경우에는 primary constructor
를 사용하고 constructor 앞에 annotation을 붙입니다.
Constructor
class Person(firstName: String, lastName: String, age: Int)
class Person constructor(firstName: String, lastName: String, age: Int)
val person = Person(“myeongin”, “woo”, 32)
class 이름 뒤에 constructor을 붙이는 것을 primary constructor 이라고 합니다.
기본적으로 생략해도 되지만 어노테이션을 붙여야 할 필요가 있을 경우에는 primary constructor
를 사용하고 constructor 앞에 annotation을 붙입니다.
class Persion @Inject constructor(firstName: String, lastName: String, age: Int)
Constructor(init)
class Person constructor(firstName: String, lastName: String, age: Int){
init {
Log.i("foo", "firstName : " + firstName + " lastName : " + lastName)
}
}
Constructor(init)
class Person constructor(firstName: String, lastName: String, age: Int){
init {
Log.i("foo", "firstName : " + firstName + " lastName : " + lastName)
}
}
클래스가 생성되면 최초로 호출
Constructor
class Person(val name: String) {
constructor(name: String, parent: Person) : this(name)
}
Constructor
class Person(val name: String) {
constructor(name: String, parent: Person) : this(name)
}
firstConstructor 호출
Constructor
class Person(val name: String) {
constructor(name: String, parent: Person) : this(name)
}
firstConstructor 호출
Java에서 생성자가 여러개인 경우를 Kotlin 에서는 위와 같이 표현합니다.
Data class
/* Java */
public class JavaPerson {
private String firstName;
private String lastName;
private int age;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
JavaPerson that = (JavaPerson) o;
if (age != that.age) {
return false;
}
if (firstName != null ? !firstName.equals(that.firstName) : that.firstName !=
return false;
}
return lastName != null ? lastName.equals(that.lastName) : that.lastName
}
@Override
public int hashCode() {
int result = firstName != null ? firstName.hashCode() : 0;
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
result = 31 * result + age;
return result;
}
}
Data class
data class dataClassPerson(val firstName: String,
val lastName: String,val age: Int)
Data class
/* Kotlin */
class Person(val firstName: String, val lastName: String, val age: Int)
data class DataClassPerson(val firstName: String, val lastName: String,
val age: Int)
Data class
/* Kotlin */
class Person(val firstName: String, val lastName: String, val age: Int)
data class DataClassPerson(val firstName: String, val lastName: String,
val age: Int)
val person = Person("myeongin", "woo", 24)
val person2 = Person("myeongin", "woo", 24)
val dataClassPerson = DataClassPerson("myeongin", "woo", 24)
val dataClassPerson2 = DataClassPerson("myeongin", "woo", 24)
Data class
/* Kotlin */
class Person(val firstName: String, val lastName: String, val age: Int)
data class DataClassPerson(val firstName: String, val lastName: String,
val age: Int)
val person = Person("myeongin", "woo", 24)
val person2 = Person("myeongin", "woo", 24)
val dataClassPerson = DataClassPerson("myeongin", "woo", 24)
val dataClassPerson2 = DataClassPerson("myeongin", "woo", 24)
println(person == person2) //false
println(dataClassPerson == dataClassPerson2) //true
println(person.toString()) //Main$Person@76ed5528
println(dataClassPerson.toString()) //DataClassPErson(
firstName=myeongin, lastName=woo, age=24)
Data class
/* Kotlin */
data class Person(val firstName: String, val lastName: String, val age: Int)
val person = Person("myeongin", "woo", 24)
println(person.component1()) // myeongin
println(person.component2()) // woo
println(person.component3()) // 24
Data class
/* Kotlin */
data class Person(val firstName: String, val lastName: String, val age: Int)
val person = Person("myeongin", "woo", 24)
println(person.component1()) // myeongin
println(person.component2()) // woo
println(person.component3()) // 24
val (firstName, lastName, age) = person
Data class
/* Kotlin */
data class Person(val firstName: String, val lastName: String, val age: Int)
val person = Person("myeongin", "woo", 24)
println(person.component1()) // myeongin
println(person.component2()) // woo
println(person.component3()) // 24
val (firstName, lastName, age) = person
println(firstName) // myeongin
println(lastName) // woo
println(age) // 24
Data class
/* Kotlin */
data class Person(val firstName: String, val lastName: String, val age: Int)
val person = Person("myeongin", "woo", 24)
println(person.component1()) // myeongin
println(person.component2()) // woo
println(person.component3()) // 24
val (firstName, lastName, age) = person
println(firstName) // myeongin
println(lastName) // woo
println(age) // 24
val copyPerson = person.copy(age = 19)
Data class
/* Kotlin */
data class Person(val firstName: String, val lastName: String, val age: Int)
val person = Person("myeongin", "woo", 24)
println(person.component1()) // myeongin
println(person.component2()) // woo
println(person.component3()) // 24
val (firstName, lastName, age) = person
println(firstName) // myeongin
println(lastName) // woo
println(age) // 24
val copyPerson = person.copy(age = 19)
println(copyPerson) // Person(firstName=myeongin, lastName=woo, age=19)
Data class
• 제약사항
• primary constructor가 있어야 합니다.
• 1개 이상의 파라미터가 있어야 합니다.
• 파라미터는 모두 프로퍼티 이어야 합니다. (val or var)
• abstract, open, sealed, inner 가 아니어야 합니다.
• 장점
• 컴파일러가 equals(), hashCode()를 생성해줍니다.
• toString()을 만들어 줍니다.
• componentN() 함수를 제공합니다. (Destructuring)
• copy() 메소드도 만들어 줍니다.
DefaultValue
/* Java */
class JavaPerson {
private JavaPerson(JavaPersonBuilder builder) {
name = builder.name;
age = builder.age;
email = builder.email;
isAdult = builder.isAdult;
}
private String name;
private int age;
private String email;
private boolean isAdult;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
public boolean isAdult() {
return isAdult;
}
@Override
public String toString() {
return "name : " + name + ", age : " + age +
", email : " + email + ", isAdult : "+ isAdult;
}
public static class JavaPersonBuilder {
private String name = "Lezhin";
private int age = 3;
private String email = null;
private boolean isAdult = false;
public JavaPersonBuilder setName(String name) {
this.name = name;
return this;
}
public JavaPersonBuilder setAge(int age) {
this.age = age;
return this;
}
public JavaPersonBuilder setEmail(String email) {
this.email = email;
return this;
}
public JavaPersonBuilder setIsAdult(boolean isAdult) {
this.isAdult = isAdult;
return this;
}
public JavaPerson build() {
return new JavaPerson(this);
}
}
}
DefaultValue
/* Java */
System.out.println(new JavaPerson.JavaPersonBuilder()
.setName("myeongin")
.setAge(32)
.build());
DefaultValue
/* Java */
System.out.println(new JavaPerson.JavaPersonBuilder()
.setName("myeongin")
.setAge(32)
.build());
//name : myeongin, age : 32, email : null, isAdult : false
DefaultValue
data class Person(val name: String = "Lezhin", val age: Int = 3,
val email: String? = null, val isAdult: Boolean = false)
val person = Person()
println(person)
DefaultValue
data class Person(val name: String = "Lezhin", val age: Int = 3,
val email: String? = null, val isAdult: Boolean = false)
val person = Person()
println(person)
//Person(name=Lezhin, age=3, email=null, isAdult=false)
DefaultValue
data class Person(val name: String = "Lezhin", val age: Int = 3,
val email: String? = null, val isAdult: Boolean = false)
val person = Person()
println(person)
//Person(name=Lezhin, age=3, email=null, isAdult=false)
val person2 = Person("myeongin", 32)
println(person2)
DefaultValue
data class Person(val name: String = "Lezhin", val age: Int = 3,
val email: String? = null, val isAdult: Boolean = false)
val person = Person()
println(person)
//Person(name=Lezhin, age=3, email=null, isAdult=false)
val person2 = Person("myeongin", 32)
println(person2)
//Person(name=myeongin, age=32, email=null, isAdult=false)
DefaultValue
data class Person(val name: String = "Lezhin", val age: Int = 3,
val email: String? = null, val isAdult: Boolean = false)
val person = Person()
println(person)
//Person(name=Lezhin, age=3, email=null, isAdult=false)
val person2 = Person("myeongin", 32)
println(person2)
//Person(name=myeongin, age=32, email=null, isAdult=false)
val person3 = Person(age=32, isAdult = true)
println(person3)
DefaultValue
data class Person(val name: String = "Lezhin", val age: Int = 3,
val email: String? = null, val isAdult: Boolean = false)
val person = Person()
println(person)
//Person(name=Lezhin, age=3, email=null, isAdult=false)
val person2 = Person("myeongin", 32)
println(person2)
//Person(name=myeongin, age=32, email=null, isAdult=false)
val person3 = Person(age=32, isAdult = true)
println(person3)
//Person(name=Lezhin, age=32, email=null, isAdult=true)
Class(enum)
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
Class(enum)
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
Class(enum)
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
val green: Color = Color.GREEN
val red: Color = Color.valueOf("RED")
val colorList: Array<Color> = Color.values()
Class(inheritance)
enum class ProtocolState {
WAITING {
override fun signal() = TALKING
},
TALKING {
override fun signal() = WAITING
};
abstract fun signal(): ProtocolState
}
Class(inheritance)
• Kotlin의 class는 기본적으로 final 입니다.
• final class는 상속이 되지 않습니다.
Class(inheritance)
• Kotlin의 class는 기본적으로 final 입니다.
• final class는 상속이 되지 않습니다.
• override 할 메소드에는 override 키워드를 붙여줍니다.(Java의
@Override와 동일)
Class(inheritance)
• Kotlin의 class는 기본적으로 final 입니다.
• final class는 상속이 되지 않습니다.
• override 할 메소드에는 override 키워드를 붙여줍니다.(Java의
@Override와 동일)
• 상속 가능한 class를 만드려면 open, abstract키워드를 사용합
니다.
• open과 abstract 모두 자바의 abstract와 비슷합니다.
• open class는 instance를 생성 할 수 있지만 abstract class는
instance를 생성 할 수 없습니다.
Class(open)
open class Foo {
open val x: Int get { ... }
}
class Bar1 : Foo() {
override val x: Int = ...
}
Class(open)
open class Foo {
open val x: Int get { ... }
}
class Bar1 : Foo() {
override val x: Int = ...
}
• property 도 override가 가능합니다.
Class(open)
open class Base {
open fun v() {}
fun nv() {}
}
class Derived() : Base() {
override fun v() {}
}
Class(open)
open class Base {
open fun v() {}
fun nv() {}
}
class Derived() : Base() {
override fun v() {}
}
• open이 없는 property와 function은 final 이기 때문에
override 할 수 없습니다.
Class(open)
interface Foo {
val count: Int
}
class Bar1(override val count: Int) : Foo
class Bar2 : Foo {
override var count = 0
}
Class(open)
interface Foo {
val count: Int
}
class Bar1(override val count: Int) : Foo
class Bar2 : Foo {
override var count = 0
}
• val 을 var 로도 override 할 수 있습니다. 반대는 안됩니다.
Class(abstract)
abstract class Person() {
abstract fun hello()
open fun hi() {
println("hi")
}
fun ya() {
println("ya")
}
}
Class(abstract)
abstract class Person() {
abstract fun hello()
open fun hi() {
println("hi")
}
fun ya() {
println("ya")
}
}
do Not implement
Class(abstract)
abstract class Person() {
abstract fun hello()
open fun hi() {
println("hi")
}
fun ya() {
println("ya")
}
}
do Not implement
do Implement
Class(abstract)
abstract open
override O O
implement (this) X O
Interface
interface say {
fun hello()
}
class Person(val name: String, val age: Int) : Say{
override fun hello() {
println("Hello")
}
}
Interface
interface say {
fun hello()
fun hi() { println("Hi") }
}
class Person(val name: String, val age: Int) : Say{
override fun hello() {
println("Hello")
}
}
Interface
• Kotlin은 Interface의 body도 구현 가능합니다.
interface say {
fun hello()
fun hi() { println("Hi") }
}
class Person(val name: String, val age: Int) : Say{
override fun hello() {
println("Hello")
}
}
Interface
interface Korean {
fun hello() { println("안녕") }
}
interface Newyorker {
fun hello() { println("Hello")}
}
class Person(val name: String, val age: Int) : Korean, Newyorker {
override fun hello() {
super<Korean>.hello()
super<Newyorker>.hello()
}
}
Interface
• Kotlin은 서로 다른 interface들이 같은 method 정의 및 구현
이 가능하며, 각각 호출 할 수 있습니다.
interface Korean {
fun hello() { println("안녕") }
}
interface Newyorker {
fun hello() { println("Hello")}
}
class Person(val name: String, val age: Int) : Korean, Newyorker {
override fun hello() {
super<Korean>.hello()
super<Newyorker>.hello()
}
}
Class(sealed)
/* Sealed classes */
sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
Class(sealed)
• enum class와 매우 유사합니다.
• enum은 각 object들이 같은 생성자와 같은 함수들을 갖지만, sealed
class는 object의 hierarchies 제한만 갖고 별도의 생성자와 프로퍼티를 갖
을 수 있습니다.
/* Sealed classes */
sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
Class(sealed)
/* Interface */
interface Expr
data class Const(val number: Double) : Expr
data class Sum(val e1: Expr, val e2: Expr) : Expr
object NotANumber : Expr
/* Sealed classes */
sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
Class(sealed)
• interface 로도 같은 효과를 낼 수 있습니다.
/* Interface */
interface Expr
data class Const(val number: Double) : Expr
data class Sum(val e1: Expr, val e2: Expr) : Expr
object NotANumber : Expr
/* Sealed classes */
sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()
Class(sealed)
/* Sealed classes */
fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
}
Class(sealed)
/* Sealed classes */
fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
}
/* interface */
fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
else -> throw IllegalArgumentException("error")
}
Class(sealed)
/* Sealed classes */
fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
// the `else` clause is not required because we've covered all the cases
}
/* interface */
fun eval(expr: Expr): Double = when(expr) {
is Const -> expr.number
is Sum -> eval(expr.e1) + eval(expr.e2)
NotANumber -> Double.NaN
else -> throw IllegalArgumentException("error")
// the `else` clause is not required because we've covered all the cases
}
• 비슷한 효과를 낼 수 있지만 when 표현식을 사용할 때 else를 작성 하냐 마냐 하는 차이가 있습니다.
• Sealed나 enum 을 사용 하게 되면 case 들이 명확해집니다. 따라서 생각 하지 않은 케이스(사전에 정의하지 않은 케이스
)를 사전에 방지해 주고, 사전에 정의한 case 에 대해 실수로 인한 누락을 컴파일 타임에 잡아줍니다. 즉 훨씬 안전하고
명확한 코딩을 할 수 있습니다.

Contenu connexe

Tendances

Groovy puzzlers по русски с Joker 2014
Groovy puzzlers по русски с Joker 2014Groovy puzzlers по русски с Joker 2014
Groovy puzzlers по русски с Joker 2014Baruch Sadogursky
 
Functional Patterns for the non-mathematician
Functional Patterns for the non-mathematicianFunctional Patterns for the non-mathematician
Functional Patterns for the non-mathematicianBrian Lonsdorf
 
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
Stanfy MadCode Meetup #9: Functional Programming 101 with SwiftStanfy MadCode Meetup #9: Functional Programming 101 with Swift
Stanfy MadCode Meetup #9: Functional Programming 101 with SwiftStanfy
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogrammingRichie Cotton
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212Mahmoud Samir Fayed
 
Predictably
PredictablyPredictably
Predictablyztellman
 
Introduction to Gremlin
Introduction to GremlinIntroduction to Gremlin
Introduction to GremlinMax De Marzi
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 englishssuser442080
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monadskenbot
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3sxw2k
 
하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3Kwang Yul Seo
 
하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4Kwang Yul Seo
 

Tendances (20)

Oh Composable World!
Oh Composable World!Oh Composable World!
Oh Composable World!
 
Millionways
MillionwaysMillionways
Millionways
 
Groovy puzzlers по русски с Joker 2014
Groovy puzzlers по русски с Joker 2014Groovy puzzlers по русски с Joker 2014
Groovy puzzlers по русски с Joker 2014
 
Functional Patterns for the non-mathematician
Functional Patterns for the non-mathematicianFunctional Patterns for the non-mathematician
Functional Patterns for the non-mathematician
 
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
Stanfy MadCode Meetup #9: Functional Programming 101 with SwiftStanfy MadCode Meetup #9: Functional Programming 101 with Swift
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogramming
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212
 
ScalaMeter 2014
ScalaMeter 2014ScalaMeter 2014
ScalaMeter 2014
 
Predictably
PredictablyPredictably
Predictably
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
 
Introduction to Gremlin
Introduction to GremlinIntroduction to Gremlin
Introduction to Gremlin
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 english
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3
 
하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4
 

Similaire à Kotlin class

public class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfpublic class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfarjuncp10
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodecamp Romania
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Codemotion
 
4Developers 2015: Testowanie ze Spockiem - Dominik Przybysz
4Developers 2015: Testowanie ze Spockiem - Dominik Przybysz4Developers 2015: Testowanie ze Spockiem - Dominik Przybysz
4Developers 2015: Testowanie ze Spockiem - Dominik PrzybyszPROIDEA
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Cody Engel
 
Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)ThomasHorta
 
Scala - fra newbie til ninja på en time
Scala - fra newbie til ninja på en timeScala - fra newbie til ninja på en time
Scala - fra newbie til ninja på en timekarianneberg
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
 
From java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFrom java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFabio Collini
 
First few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesFirst few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesNebojša Vukšić
 
About java
About javaAbout java
About javaJay Xu
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기Arawn Park
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018 Codemotion
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in javaHarish Gyanani
 
Hey Kotlin, How it works?
Hey Kotlin, How it works?Hey Kotlin, How it works?
Hey Kotlin, How it works?Chang W. Doh
 
Kotlin, 어떻게 동작하나요
Kotlin, 어떻게 동작하나요Kotlin, 어떻게 동작하나요
Kotlin, 어떻게 동작하나요Chang W. Doh
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlinThijs Suijten
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)HamletDRC
 

Similaire à Kotlin class (20)

Introduction kot iin
Introduction kot iinIntroduction kot iin
Introduction kot iin
 
public class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdfpublic class Person { private String name; private int age;.pdf
public class Person { private String name; private int age;.pdf
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical Groovy
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
 
4Developers 2015: Testowanie ze Spockiem - Dominik Przybysz
4Developers 2015: Testowanie ze Spockiem - Dominik Przybysz4Developers 2015: Testowanie ze Spockiem - Dominik Przybysz
4Developers 2015: Testowanie ze Spockiem - Dominik Przybysz
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)
 
Scala - fra newbie til ninja på en time
Scala - fra newbie til ninja på en timeScala - fra newbie til ninja på en time
Scala - fra newbie til ninja på en time
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
From java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFrom java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+k
 
First few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesFirst few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examples
 
About java
About javaAbout java
About java
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in java
 
Hey Kotlin, How it works?
Hey Kotlin, How it works?Hey Kotlin, How it works?
Hey Kotlin, How it works?
 
Kotlin, 어떻게 동작하나요
Kotlin, 어떻게 동작하나요Kotlin, 어떻게 동작하나요
Kotlin, 어떻게 동작하나요
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
C# 3.5 Features
C# 3.5 FeaturesC# 3.5 Features
C# 3.5 Features
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 

Plus de Myeongin Woo

DroidKnight 2018 State machine by Selaed class
DroidKnight 2018 State machine by Selaed classDroidKnight 2018 State machine by Selaed class
DroidKnight 2018 State machine by Selaed classMyeongin Woo
 
Lezhin kotlin jetbrain
Lezhin kotlin jetbrainLezhin kotlin jetbrain
Lezhin kotlin jetbrainMyeongin Woo
 
토이 프로젝트를 하자.Pptx
토이 프로젝트를 하자.Pptx토이 프로젝트를 하자.Pptx
토이 프로젝트를 하자.PptxMyeongin Woo
 

Plus de Myeongin Woo (8)

Goodbye null
Goodbye nullGoodbye null
Goodbye null
 
Fp basic-kotlin
Fp basic-kotlinFp basic-kotlin
Fp basic-kotlin
 
DroidKnight 2018 State machine by Selaed class
DroidKnight 2018 State machine by Selaed classDroidKnight 2018 State machine by Selaed class
DroidKnight 2018 State machine by Selaed class
 
Lezhin kotlin jetbrain
Lezhin kotlin jetbrainLezhin kotlin jetbrain
Lezhin kotlin jetbrain
 
Kotlin expression
Kotlin expressionKotlin expression
Kotlin expression
 
Kotlin with fp
Kotlin with fpKotlin with fp
Kotlin with fp
 
토이 프로젝트를 하자.Pptx
토이 프로젝트를 하자.Pptx토이 프로젝트를 하자.Pptx
토이 프로젝트를 하자.Pptx
 
Kotlin.md
Kotlin.mdKotlin.md
Kotlin.md
 

Dernier

Copilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotCopilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotEdgard Alejos
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jNeo4j
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 

Dernier (20)

Copilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotCopilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform Copilot
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 

Kotlin class

  • 2. Class /* Java */ public class PersonJava { private final String name; public PersonJava(String name) { this.name = name; } public String getName() { return name; } }
  • 3. Class /* Java */ Person person = new Person("myeonginWoo"); System.out.println(person.getName());
  • 4. Class /* Kotlin */ class Person(val name: String)
  • 5. Class /* Kotlin */ val person = Person("myeonginWoo") println(person.name)
  • 6. Class /* Kotlin */ val person = Person("myeonginWoo") println(person.name) New 연산자 필요 없음. getName() 이 아님
  • 7. Property class Person(firstName: String, lastName: String, age: Int) class Person2(val firstName: String, val lastName: String, val age: Int)
  • 8. Property class Person(firstName: String, lastName: String, age: Int) class Person2(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 32 ) val person2 = Person2("myeongin", "woo", 32 )
  • 9. Property class Person(firstName: String, lastName: String, age: Int) class Person2(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 32 ) val person2 = Person2("myeongin", "woo", 32 ) println(person.firstName) // Error println(person2.firstName) // Ok
  • 10. Property class Person(firstName: String, lastName: String, age: Int) class Person2(val firstName: String, val lastName: String, val age: Int) • Java의 class 내부에 선언되는 field와 비슷하지만 다릅니다. • field는 별도로 getter, setter를 선언해줘야 합니다. • propertry는 getter와 setter를 자동으로 생성해 줍니다. • public, private, protected 와 같은 접근제어자만 잘 사용한다면 보일러플레이트들이 많 이 제거되어 실제 로직에 더 집중 할 수 있습니다.
  • 11. Property /* Java */ public class PersonJava { String firstNam; String lastName; int age; public PersonJava(String firstName, String lastName, int age) { this.firstNam = firstName; this.lastName = lastName; this.age = age; } public String getFirstNam() { return firstNam; } public String getLastName() { return lastName; } public int getAge() { return age; } public void setFirstNam(String firstNam){ this.firstNam = firstNam; } public void setLastName(String lastName){ this.lastName = lastName; } public void setAge(int age){ this.age = age; }
  • 12. Property /* Java */ public class PersonJava { String firstNam; String lastName; int age; public PersonJava(String firstName, String lastName, int age) { this.firstNam = firstName; this.lastName = lastName; this.age = age; } public String getFirstNam() { return firstNam; } public String getLastName() { return lastName; } public int getAge() { return age; } public void setFirstNam(String firstNam){ this.firstNam = firstNam; } public void setLastName(String lastName){ this.lastName = lastName; } public void setAge(int age){ this.age = age; } /* kotlin */ class PersonKotlin( var firstName: String, var lastName: String, var age: Int)
  • 13. Property class Person { var name: String = "" get() = field.toUpperCase() set(value) { field = "Name : " + value } }
  • 14. Property class Person { var name: String = "" get() = field.toUpperCase() set(value) { field = "Name : " + value } } person.name = "test"
  • 15. Property class Person { var name: String = "" get() = field.toUpperCase() set(value) { field = "Name : " + value } } person.name = "test" println(person.name) //NAME : TEST
  • 16. Constructor class Person(firstName: String, lastName: String, age: Int) class Person constructor(firstName: String, lastName: String, age: Int) val person = Person(“myeongin”, “woo”, 32)
  • 17. Constructor class Person(firstName: String, lastName: String, age: Int) class Person constructor(firstName: String, lastName: String, age: Int) val person = Person(“myeongin”, “woo”, 32) class 이름 뒤에 constructor을 붙이는 것을 primary constructor 이라고 합니다. 기본적으로 생략해도 되지만 어노테이션을 붙여야 할 필요가 있을 경우에는 primary constructor 를 사용하고 constructor 앞에 annotation을 붙입니다.
  • 18. Constructor class Person(firstName: String, lastName: String, age: Int) class Person constructor(firstName: String, lastName: String, age: Int) val person = Person(“myeongin”, “woo”, 32) class 이름 뒤에 constructor을 붙이는 것을 primary constructor 이라고 합니다. 기본적으로 생략해도 되지만 어노테이션을 붙여야 할 필요가 있을 경우에는 primary constructor 를 사용하고 constructor 앞에 annotation을 붙입니다. class Persion @Inject constructor(firstName: String, lastName: String, age: Int)
  • 19. Constructor(init) class Person constructor(firstName: String, lastName: String, age: Int){ init { Log.i("foo", "firstName : " + firstName + " lastName : " + lastName) } }
  • 20. Constructor(init) class Person constructor(firstName: String, lastName: String, age: Int){ init { Log.i("foo", "firstName : " + firstName + " lastName : " + lastName) } } 클래스가 생성되면 최초로 호출
  • 21. Constructor class Person(val name: String) { constructor(name: String, parent: Person) : this(name) }
  • 22. Constructor class Person(val name: String) { constructor(name: String, parent: Person) : this(name) } firstConstructor 호출
  • 23. Constructor class Person(val name: String) { constructor(name: String, parent: Person) : this(name) } firstConstructor 호출 Java에서 생성자가 여러개인 경우를 Kotlin 에서는 위와 같이 표현합니다.
  • 24. Data class /* Java */ public class JavaPerson { private String firstName; private String lastName; private int age; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } JavaPerson that = (JavaPerson) o; if (age != that.age) { return false; } if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != return false; } return lastName != null ? lastName.equals(that.lastName) : that.lastName } @Override public int hashCode() { int result = firstName != null ? firstName.hashCode() : 0; result = 31 * result + (lastName != null ? lastName.hashCode() : 0); result = 31 * result + age; return result; } }
  • 25. Data class data class dataClassPerson(val firstName: String, val lastName: String,val age: Int)
  • 26. Data class /* Kotlin */ class Person(val firstName: String, val lastName: String, val age: Int) data class DataClassPerson(val firstName: String, val lastName: String, val age: Int)
  • 27. Data class /* Kotlin */ class Person(val firstName: String, val lastName: String, val age: Int) data class DataClassPerson(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 24) val person2 = Person("myeongin", "woo", 24) val dataClassPerson = DataClassPerson("myeongin", "woo", 24) val dataClassPerson2 = DataClassPerson("myeongin", "woo", 24)
  • 28. Data class /* Kotlin */ class Person(val firstName: String, val lastName: String, val age: Int) data class DataClassPerson(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 24) val person2 = Person("myeongin", "woo", 24) val dataClassPerson = DataClassPerson("myeongin", "woo", 24) val dataClassPerson2 = DataClassPerson("myeongin", "woo", 24) println(person == person2) //false println(dataClassPerson == dataClassPerson2) //true println(person.toString()) //Main$Person@76ed5528 println(dataClassPerson.toString()) //DataClassPErson( firstName=myeongin, lastName=woo, age=24)
  • 29. Data class /* Kotlin */ data class Person(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 24) println(person.component1()) // myeongin println(person.component2()) // woo println(person.component3()) // 24
  • 30. Data class /* Kotlin */ data class Person(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 24) println(person.component1()) // myeongin println(person.component2()) // woo println(person.component3()) // 24 val (firstName, lastName, age) = person
  • 31. Data class /* Kotlin */ data class Person(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 24) println(person.component1()) // myeongin println(person.component2()) // woo println(person.component3()) // 24 val (firstName, lastName, age) = person println(firstName) // myeongin println(lastName) // woo println(age) // 24
  • 32. Data class /* Kotlin */ data class Person(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 24) println(person.component1()) // myeongin println(person.component2()) // woo println(person.component3()) // 24 val (firstName, lastName, age) = person println(firstName) // myeongin println(lastName) // woo println(age) // 24 val copyPerson = person.copy(age = 19)
  • 33. Data class /* Kotlin */ data class Person(val firstName: String, val lastName: String, val age: Int) val person = Person("myeongin", "woo", 24) println(person.component1()) // myeongin println(person.component2()) // woo println(person.component3()) // 24 val (firstName, lastName, age) = person println(firstName) // myeongin println(lastName) // woo println(age) // 24 val copyPerson = person.copy(age = 19) println(copyPerson) // Person(firstName=myeongin, lastName=woo, age=19)
  • 34. Data class • 제약사항 • primary constructor가 있어야 합니다. • 1개 이상의 파라미터가 있어야 합니다. • 파라미터는 모두 프로퍼티 이어야 합니다. (val or var) • abstract, open, sealed, inner 가 아니어야 합니다. • 장점 • 컴파일러가 equals(), hashCode()를 생성해줍니다. • toString()을 만들어 줍니다. • componentN() 함수를 제공합니다. (Destructuring) • copy() 메소드도 만들어 줍니다.
  • 35. DefaultValue /* Java */ class JavaPerson { private JavaPerson(JavaPersonBuilder builder) { name = builder.name; age = builder.age; email = builder.email; isAdult = builder.isAdult; } private String name; private int age; private String email; private boolean isAdult; public String getName() { return name; } public int getAge() { return age; } public String getEmail() { return email; } public boolean isAdult() { return isAdult; } @Override public String toString() { return "name : " + name + ", age : " + age + ", email : " + email + ", isAdult : "+ isAdult; } public static class JavaPersonBuilder { private String name = "Lezhin"; private int age = 3; private String email = null; private boolean isAdult = false; public JavaPersonBuilder setName(String name) { this.name = name; return this; } public JavaPersonBuilder setAge(int age) { this.age = age; return this; } public JavaPersonBuilder setEmail(String email) { this.email = email; return this; } public JavaPersonBuilder setIsAdult(boolean isAdult) { this.isAdult = isAdult; return this; } public JavaPerson build() { return new JavaPerson(this); } } }
  • 36. DefaultValue /* Java */ System.out.println(new JavaPerson.JavaPersonBuilder() .setName("myeongin") .setAge(32) .build());
  • 37. DefaultValue /* Java */ System.out.println(new JavaPerson.JavaPersonBuilder() .setName("myeongin") .setAge(32) .build()); //name : myeongin, age : 32, email : null, isAdult : false
  • 38. DefaultValue data class Person(val name: String = "Lezhin", val age: Int = 3, val email: String? = null, val isAdult: Boolean = false) val person = Person() println(person)
  • 39. DefaultValue data class Person(val name: String = "Lezhin", val age: Int = 3, val email: String? = null, val isAdult: Boolean = false) val person = Person() println(person) //Person(name=Lezhin, age=3, email=null, isAdult=false)
  • 40. DefaultValue data class Person(val name: String = "Lezhin", val age: Int = 3, val email: String? = null, val isAdult: Boolean = false) val person = Person() println(person) //Person(name=Lezhin, age=3, email=null, isAdult=false) val person2 = Person("myeongin", 32) println(person2)
  • 41. DefaultValue data class Person(val name: String = "Lezhin", val age: Int = 3, val email: String? = null, val isAdult: Boolean = false) val person = Person() println(person) //Person(name=Lezhin, age=3, email=null, isAdult=false) val person2 = Person("myeongin", 32) println(person2) //Person(name=myeongin, age=32, email=null, isAdult=false)
  • 42. DefaultValue data class Person(val name: String = "Lezhin", val age: Int = 3, val email: String? = null, val isAdult: Boolean = false) val person = Person() println(person) //Person(name=Lezhin, age=3, email=null, isAdult=false) val person2 = Person("myeongin", 32) println(person2) //Person(name=myeongin, age=32, email=null, isAdult=false) val person3 = Person(age=32, isAdult = true) println(person3)
  • 43. DefaultValue data class Person(val name: String = "Lezhin", val age: Int = 3, val email: String? = null, val isAdult: Boolean = false) val person = Person() println(person) //Person(name=Lezhin, age=3, email=null, isAdult=false) val person2 = Person("myeongin", 32) println(person2) //Person(name=myeongin, age=32, email=null, isAdult=false) val person3 = Person(age=32, isAdult = true) println(person3) //Person(name=Lezhin, age=32, email=null, isAdult=true)
  • 44. Class(enum) enum class Direction { NORTH, SOUTH, WEST, EAST }
  • 45. Class(enum) enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) }
  • 46. Class(enum) enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } val green: Color = Color.GREEN val red: Color = Color.valueOf("RED") val colorList: Array<Color> = Color.values()
  • 47. Class(inheritance) enum class ProtocolState { WAITING { override fun signal() = TALKING }, TALKING { override fun signal() = WAITING }; abstract fun signal(): ProtocolState }
  • 48. Class(inheritance) • Kotlin의 class는 기본적으로 final 입니다. • final class는 상속이 되지 않습니다.
  • 49. Class(inheritance) • Kotlin의 class는 기본적으로 final 입니다. • final class는 상속이 되지 않습니다. • override 할 메소드에는 override 키워드를 붙여줍니다.(Java의 @Override와 동일)
  • 50. Class(inheritance) • Kotlin의 class는 기본적으로 final 입니다. • final class는 상속이 되지 않습니다. • override 할 메소드에는 override 키워드를 붙여줍니다.(Java의 @Override와 동일) • 상속 가능한 class를 만드려면 open, abstract키워드를 사용합 니다. • open과 abstract 모두 자바의 abstract와 비슷합니다. • open class는 instance를 생성 할 수 있지만 abstract class는 instance를 생성 할 수 없습니다.
  • 51. Class(open) open class Foo { open val x: Int get { ... } } class Bar1 : Foo() { override val x: Int = ... }
  • 52. Class(open) open class Foo { open val x: Int get { ... } } class Bar1 : Foo() { override val x: Int = ... } • property 도 override가 가능합니다.
  • 53. Class(open) open class Base { open fun v() {} fun nv() {} } class Derived() : Base() { override fun v() {} }
  • 54. Class(open) open class Base { open fun v() {} fun nv() {} } class Derived() : Base() { override fun v() {} } • open이 없는 property와 function은 final 이기 때문에 override 할 수 없습니다.
  • 55. Class(open) interface Foo { val count: Int } class Bar1(override val count: Int) : Foo class Bar2 : Foo { override var count = 0 }
  • 56. Class(open) interface Foo { val count: Int } class Bar1(override val count: Int) : Foo class Bar2 : Foo { override var count = 0 } • val 을 var 로도 override 할 수 있습니다. 반대는 안됩니다.
  • 57. Class(abstract) abstract class Person() { abstract fun hello() open fun hi() { println("hi") } fun ya() { println("ya") } }
  • 58. Class(abstract) abstract class Person() { abstract fun hello() open fun hi() { println("hi") } fun ya() { println("ya") } } do Not implement
  • 59. Class(abstract) abstract class Person() { abstract fun hello() open fun hi() { println("hi") } fun ya() { println("ya") } } do Not implement do Implement
  • 61. Interface interface say { fun hello() } class Person(val name: String, val age: Int) : Say{ override fun hello() { println("Hello") } }
  • 62. Interface interface say { fun hello() fun hi() { println("Hi") } } class Person(val name: String, val age: Int) : Say{ override fun hello() { println("Hello") } }
  • 63. Interface • Kotlin은 Interface의 body도 구현 가능합니다. interface say { fun hello() fun hi() { println("Hi") } } class Person(val name: String, val age: Int) : Say{ override fun hello() { println("Hello") } }
  • 64. Interface interface Korean { fun hello() { println("안녕") } } interface Newyorker { fun hello() { println("Hello")} } class Person(val name: String, val age: Int) : Korean, Newyorker { override fun hello() { super<Korean>.hello() super<Newyorker>.hello() } }
  • 65. Interface • Kotlin은 서로 다른 interface들이 같은 method 정의 및 구현 이 가능하며, 각각 호출 할 수 있습니다. interface Korean { fun hello() { println("안녕") } } interface Newyorker { fun hello() { println("Hello")} } class Person(val name: String, val age: Int) : Korean, Newyorker { override fun hello() { super<Korean>.hello() super<Newyorker>.hello() } }
  • 66. Class(sealed) /* Sealed classes */ sealed class Expr data class Const(val number: Double) : Expr() data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr()
  • 67. Class(sealed) • enum class와 매우 유사합니다. • enum은 각 object들이 같은 생성자와 같은 함수들을 갖지만, sealed class는 object의 hierarchies 제한만 갖고 별도의 생성자와 프로퍼티를 갖 을 수 있습니다. /* Sealed classes */ sealed class Expr data class Const(val number: Double) : Expr() data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr()
  • 68. Class(sealed) /* Interface */ interface Expr data class Const(val number: Double) : Expr data class Sum(val e1: Expr, val e2: Expr) : Expr object NotANumber : Expr /* Sealed classes */ sealed class Expr data class Const(val number: Double) : Expr() data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr()
  • 69. Class(sealed) • interface 로도 같은 효과를 낼 수 있습니다. /* Interface */ interface Expr data class Const(val number: Double) : Expr data class Sum(val e1: Expr, val e2: Expr) : Expr object NotANumber : Expr /* Sealed classes */ sealed class Expr data class Const(val number: Double) : Expr() data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr()
  • 70. Class(sealed) /* Sealed classes */ fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN }
  • 71. Class(sealed) /* Sealed classes */ fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN } /* interface */ fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN else -> throw IllegalArgumentException("error") }
  • 72. Class(sealed) /* Sealed classes */ fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN // the `else` clause is not required because we've covered all the cases } /* interface */ fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN else -> throw IllegalArgumentException("error") // the `else` clause is not required because we've covered all the cases } • 비슷한 효과를 낼 수 있지만 when 표현식을 사용할 때 else를 작성 하냐 마냐 하는 차이가 있습니다. • Sealed나 enum 을 사용 하게 되면 case 들이 명확해집니다. 따라서 생각 하지 않은 케이스(사전에 정의하지 않은 케이스 )를 사전에 방지해 주고, 사전에 정의한 case 에 대해 실수로 인한 누락을 컴파일 타임에 잡아줍니다. 즉 훨씬 안전하고 명확한 코딩을 할 수 있습니다.