-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Week1][입문편] 12강 코틀린에서 object 키워드를 다루는 방법 (#2)
- Loading branch information
Showing
8 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
임준형/kotlin-grammer/src/lec12/objectANDsingleton/JavaPerson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package lec12.objectANDsingleton; | ||
|
||
public class JavaPerson { | ||
private static final int MIN_AGE = 1; | ||
|
||
private String name; | ||
private int age; | ||
|
||
private JavaPerson(final String name, final int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public static JavaPerson from(final String name) { | ||
return new JavaPerson(name, MIN_AGE); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
임준형/kotlin-grammer/src/lec12/objectANDsingleton/JavaSingleton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package lec12.objectANDsingleton; | ||
|
||
public class JavaSingleton { | ||
private static final JavaSingleton INSTANCE = new JavaSingleton(); | ||
|
||
private JavaSingleton() { | ||
} | ||
|
||
public static JavaSingleton getInstance() { | ||
return INSTANCE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package lec12.objectANDsingleton | ||
|
||
interface Log { | ||
fun log(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package lec12.objectANDsingleton | ||
|
||
class Person private constructor( | ||
private var name: String, | ||
private var age: Int | ||
) { | ||
|
||
// static 대신 companion object 사용 | ||
// const 작성시 컴파일 시에 값이 들어감. 정말 진짜 상수 | ||
// val 작성시 런타임 시에 값이 들어감. | ||
companion object Factory : Log { | ||
private const val MIN_AGE = 1 | ||
fun newBaby(name: String): Person { | ||
return Person(name, MIN_AGE) | ||
} | ||
|
||
override fun log() { | ||
println("나는 Person 클래스 동행 객체에요") | ||
} | ||
} | ||
} | ||
|
||
// 외부에서 바로 접근 가능 | ||
object Singleton { | ||
var a:Int = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package lec12.staticclass; | ||
|
||
public class JavaMain { | ||
|
||
public static void main(String[] args) { | ||
moveSomething(new Moveable() { | ||
@Override | ||
public void move() { | ||
System.out.println("움직인다"); | ||
} | ||
|
||
@Override | ||
public void fly() { | ||
System.out.println("난다"); | ||
} | ||
}); | ||
} | ||
|
||
private static void moveSomething(Moveable moveable) { | ||
moveable.move(); | ||
moveable.fly(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package lec12.staticclass | ||
|
||
import lec12.objectANDsingleton.Person | ||
|
||
fun main() { | ||
moveSomething(object : Moveable { | ||
override fun move() { | ||
println("움직인다") | ||
} | ||
|
||
override fun fly() { | ||
println("난다") | ||
} | ||
}) | ||
|
||
Person.newBaby("hello") | ||
} | ||
|
||
private fun moveSomething(moveable: Moveable) { | ||
moveable.move() | ||
moveable.fly() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lec12.staticclass; | ||
|
||
public interface Moveable { | ||
|
||
void move(); | ||
|
||
void fly(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters