6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

KotlinでAbstractClassをnewする

Last updated at Posted at 2016-03-30

(kotlinでnewするっていう表現良くないかも...

Javaでたまにやるコレ

抽象クラスFoobarの匿名サブクラスを実装して,インスタンスを生成するやつ

java
abstract class FooBar {
	public void hogehoge();
}

FooBar foobar = new FooBar() {
		public void hogehoge() {
			...
		}
	}

Kotlinではobjectというキーワードを用いて行う

kotlin
abstract class FooBar {
	fun hogehoge()
}

val foobar = object: FooBar() {
		override fun hogehoge() {
			...
		}
	}

参考

http://stackoverflow.com/questions/34143564/create-an-instance-of-an-abstract-class-in-kotlin

http://kotlin.hatenablog.jp/entry/2012/12/19/212600

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?