7
7

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.

Enumのabstract method

Posted at

※Qiita始めたのでHatenaやめようと思ったので昔の写しです。

javaのEnumってabstract methodが書けるね。

こんなのだね。

public class Otameshi {
	public enum E {
		A {
			@Override
			void method() {
				System.out.println("A method");
			}
		},
		B {
			@Override
			void method() {
				System.out.println("B method");
			}
		};

		abstract void method();
	}

	public static void main(String[] args) {
		for (E e : E.values()) {
			e.method();
		}
	}
}

で、なんとなく気になってクラスファイルを覗いたら案の定クラスがEnum宣言分増えてたよ。
つまり、

public class Otameshi {
	public enum E {
		A {
			@Override
			void method() {
				System.out.println("A method");
			}
		},
		B {
			@Override
			void method() {
				System.out.println("B method");
			}
		};

		abstract void method();
	}

	public static void main(String[] args) {
		for (E e : E.values()) {
			System.out.println(e.getClass());
		}
	}
}

ってやると

class Otameshi$E$1
class Otameshi$E$2

ってなる。

7
7
2

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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?