Anonymous inner classes / lambda expressions

Anonymous inner classes in java summary

Anonymous inner classes is the name of the internal class

Because there is no name, so anonymous inner classes can only be used once, it is usually used to simplify writing code

However, the use of anonymous inner classes there is a prerequisite: you must inherit from a parent class or implement an interface

 

Example 1: do not use anonymous inner classes to implement the abstract methods

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

abstract class Person {

    public abstract void eat();

}

 

class Child extends Person {

    public void eat() {

        System.out.println("eat something");

    }

}

 

public class Demo {

    public static void main(String[] args) {

        Person p = new Child();

        p.eat();

    }

}

Operating results: EAT something

You can see that we inherited with Child Person class, and then implement an instance of the Child, which upcast reference to the Person class

However, if the Child class here is used only once, then it is written as a separate category would not be too much trouble?

This time on the introduction of anonymous inner classes

 

Example 2: basically anonymous inner classes

1

2

3

4

5

6

7

8

9

10

11

12

13

14

abstract class Person {

    public abstract void eat();

}

 

public class Demo {

    public static void main(String[] args) {

        Person p = new Person() {

            public void eat() {

                System.out.println("eat something");

            }

        };

        p.eat();

    }

}

Operating results: EAT something

You can see, we direct method in abstract class Person implements in braces

So that you can omit a writing class

Further, the anonymous inner classes can also be used on the interface

 

Example 3: Use anonymous inner classes on the interface

interface Person {

    public void eat();

}

 

public class Demo {

    public static void main(String[] args) {

        Person p = new Person() {

            public void eat() {

                System.out.println("eat something");

            }

        };

        p.eat();

    }

}

Operating results: EAT something

 

As can be seen from the above example, as long as a class or an interface is abstract, the method which subclass may be used to implement anonymous inner classes

The most common situation is on the multi-threaded, multi-threaded because to have to inherit the Thread class or Runnable interface inheritance

 

Example 4: Thread class anonymous inner class

public class Demo {

    public static void main(String[] args) {

        Thread t = new Thread() {

            public void run() {

                for (int i = 1; i <= 5; i++) {

                    System.out.print(i + " ");

                }

            }

        };

        t.start();

    }

}

Run Results: 12,345

 

Example 5: Runnable interface implemented anonymous inner classes

1

2

3

4

5

6

7

8

9

10

11

12

13

public class Demo {

    public static void main(String[] args) {

        Runnable r = new Runnable() {

            public void run() {

                for (int i = 1; i <= 5; i++) {

                    System.out.print(i + " ");

                }

            }

        };

        Thread t = new Thread(r);

        t.start();

    }

}

Run Results: 12,345

******************************************************

lambda expressions 

* Use Lambda expressions simplify thread (once) a 
* Lamda expression is only suitable interface has a method of time if it is both a method will be a problem
package com.thread;

/**
 *  Lambda 表达式 简化线程(用一次)的使用
 *  Lamda表达式  只适合接口 有一个方法的时候   如果是俩个方法 就会出问题
 */
public class LambdaThread {



    //静态内部类 内部类的好处  如果外部类不访问 就不会被加载 节省资源
    static class Test implements  Runnable{

        @Override
        public void run() {
            for (int i = 0; i <20; i++) {
                System.out.println("一边听歌");
            }
        }
    }

    public static void main(String[] args) {
        //最初的方法
        //new Thread(new Test()).start();
        //局部内部类
        class Test2 implements Runnable{
            public void run() {
                for(int i=0;i<20;i++) {
                    System.out.println("一边听歌");
                }
            }
        }
        new Thread(new Test2()).start(); //写的还是不太方便

        //匿名内部类   必须借助接口或者父类
        //如果是接口 必须实现方法 不然会报错
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println("一般听哦");
                }
            }
        }).start();   //也是不太方便

        //jdk8 简化  lambda表达式  只有简单的类或 接口才会使用lambda表达式 目的是为了简化操作
        //  接口里面 只有一个方法的可以推到出来 要是俩个的话 就无法识别了
       new Thread(()->{
           for (int i = 0; i < 20; i++) {
               System.out.println("我是lambda 表达式  哈哈哈");
           }
       }).start();
    }
}

 

Published 19 original articles · won praise 8 · views 4131

Guess you like

Origin blog.csdn.net/paohui001lqp/article/details/100994686