Android | Generic
페이지 정보
- 작성자:
- 핵탐
- 작성일
- 01.11 11:48
- 조회
- 2,493
- 댓글
- 0
본문
class 생성자에서 Generic 사용하기
class ClassName<T[:Type]> (val t: T) {
...
}
function 에서 Generic 사용하기
fun <t[:Type]> FunctionName (t: T) {
...
}
사용예
[code]
fun main() {
println("-- class contructor --")
UsingGeneric(A()).doShouting()
UsingGeneric(B()).doShouting()
UsingGeneric(C()).doShouting()
println("-- function --")
doShouting(A())
doShouting(B())
doShouting(C())
}
fun <T: A>doShouting(t: T) {
t.shout()
}
open class A {
open fun shout() {
println("A가 소리칩니다")
}
}
class B: A() {
override fun shout() {
println("B가 소리칩니다")
}
}
class C: A() {
override fun shout() {
println("C가 소리칩니다")
}
}
class UsingGeneric<T: A>(val t: T) {
fun doShouting() {
t.shout()
}
}
[/code]
댓글 0개
등록된 댓글이 없습니다.