Data class - java
public final class UserInfo {
private String name;
private int age;
public UserInfo(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "UserInfo(name = " +this.name + ", age" + this.age + ")";
}
public int hashCode() {
return (this.name != null?this.name.hashCode():0) * 31 + this.age;
}
public boolean equals(Object var1) {
if(obj == this) return true;
if(!(obj instanceof UserInfo)) {
return false;
}
UserInfo userInfo = (UserInfo) obj;
return userInfo.name.equals(name) &&
userInfo.age == age;
}
}
Data class - kotlin
data class UserInfo(var name : String?, var age : Int?)
싱글톤 구현하기 - Eager initialization
public class Eager {
private static Eager instance = new Eager();
private Eager() {}
public static Eager getInstance() {
return instance;
}
}
public class Lazy {
private static Lazy instance = null;
private Lazy() {}
public static Lazy getInstance() {
if (instance == null) {
instance = new Lazy();
}
return instance;
}
}
// thread safe initialization
public class ThreadSafe {
private static ThreadSafe instance = null;
private ThreadSafe() {
}
public static ThreadSafe getInstance() {
if (instance == null) {
synchronized (ThreadSafe.class) {
if (instance == null) {
instance = new ThreadSafe();
}
}
}
return instance;
}
}
싱글톤 구현하기 - kotlin object
object Eager
- 생성자를 가질 수 없다.
싱글톤 구현하기 - kotlin
class Singleton private constructor(val name : String) {
companion object {
@Volatile private var INSTANCE : Singleton? = null
fun getInstance(name : String) : Singleton =
INSTANCE ?: synchronized(this) {
INSTANCE ?: Singleton(name).also { INSTANCE = it }
}
}
}
// Lazy 패턴 사용해서 싱글톤 구현
class Lazy {
companion object {
private val INSTANCE : Lazy by lazy {
Lazy()
}
@JvmStatic fun getInstance() = INSTANCE
}
}
// 하지만 값을 넘겨주거나 하지는 못함!!
class 안에 class 정의하기 - Nested class - java에서는 static
class Outer {
private val bar : Int = 1
class Nested { // inner 안써주면 static 써준거와 같음.
fun foo() = 2
}
}
val demo = Outer.Nested().foo() // == 2 / Outer 클래스 객체 선언 안해줌
// inner 추가
class Outer {
private val bar : Int = 1
inner class Inner{
fun foo() = bar // inner 클래스 정의해줘서 bar 접근 가능
}
}
val demo = Outer().Inner().foo() // == 1 / Outer 클래스 객체 선언 해줘야됨!!
Anonymous inner classes 익명 클래스
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView : RecyclerView?, dx : Int, dy : Int) {
super.onScrolled(recyclerView, dx, dy)
}
override fun onScrollStateChanged(recyclerView : RecyclerView?, newState : Int) {
super.onScrollStateChanged(recyclerView, newState)
}
}
'# 02 > 코틀린' 카테고리의 다른 글
[Kotlin] 유용한 kotiln Standard 라이브러리 (0) | 2019.07.12 |
---|---|
[Kotlin] Higher-Order Functions (0) | 2019.07.12 |
[Kotlin] Sealed Classes (0) | 2019.07.12 |
[Kotlin] Companion Object (0) | 2019.07.12 |
[Kotlin] Class initializer (0) | 2019.07.12 |