(同じ利用者による、間の5版が非表示)
21行目: 21行目:


==3. 匿名クラス==
==3. 匿名クラス==
親クラス名を利用してインスタンス化と定義を同時にしてしまう。使い捨てクラス
スーパークラス名を利用してインスタンス化と定義を同時にしてしまう。使い捨てクラス。ただ、匿名クラスを使うよりも [[ラムダ式]] を使う方がスマート。
  public class Main {
  public class Main {
     public static void main(String[] args) {
     public static void main(String[] args) {
         pocket<Object> poc = new Pocket<>();
         pocket<Object> poc = new Pocket<>();
         System.out.println("Make Anonymous Instance and into the Pocket.");
         System.out.println("Make Anonymous Instance and into the Pocket at a same time.");
         poc.put(<u>new Object()</u> {
         poc.put(<u>new Object()</u> {
             String field;
             String field;

2019年7月16日 (火) 15:25時点における最新版

< Java基本文法

1. メンバクラス

クラスブロック内部、フィールドやメソッドと一緒に定義(static有無で用途が違う)

2. ローカルクラス

メソッド内部に定義。そのメソッド内でのみ複数回使いたい時に利用

public class Outer {
    int field;
    void method() {
        int a = 10;
        class Inner {
            public void innerMethod() {
                System.out.println("This is innerMethod.");
            }
        }
        Inner inn = new Inner();
        inn.innerMethod();
    }
}

3. 匿名クラス

スーパークラス名を利用してインスタンス化と定義を同時にしてしまう。使い捨てクラス。ただ、匿名クラスを使うよりも ラムダ式 を使う方がスマート。

public class Main {
    public static void main(String[] args) {
        pocket<Object> poc = new Pocket<>();
        System.out.println("Make Anonymous Instance and into the Pocket at a same time.");
        poc.put(new Object() {
            String field;
            void method() {...}
        });
    }
}