Java8 Simple anonymous inner class using Demo__java

Source: Internet
Author: User

1, no return value V1 version

/** * Java8 Concise anonymous inner class using demo * Created by Wangyingjie1 on 2017/2/23.
    */public class Executehelper {public static void execute (EXECUTE) {execute.doexecute ();
        public static void Execute (Execute execute, String umpkey) {System.out.println ("start========" + Umpkey);
        try {execute.doexecute ();
        catch (Exception e) {System.out.println ("umpkey===>" + Umpkey);
    } System.out.println ("end========" + Umpkey);
    Private interface Execute {void Doexecute ();
        //test use of anonymous inner classes public static void main (string[] args) {Executehelper.execute (()-> study.print ());

        Executehelper.execute (()-> study.print (), "xxx");
        The output of the test array string[] profiles = {"xxx", "yyyyy", "zzzzzzzzzzzz"};
        String profilesstr = Arrays.stream (profiles). Reduce ((A, B)-> A + "," + B). get (); System.out.println (profilesstr + "=======\n" + arrays.toString (profiles)); public static class Study {public static void print () {System.out.println ("Hello world*******
        "); }
    }
}

2, with return value V2 version

/** * Java8 Concise anonymous inner class using demo * Created by Wangyingjie1 on 2017/2/23.
    */public class Executehelper {public static void execute (EXECUTE) {execute.doexecute (); 
     /** * with return-worthy execution method, static weave to implement code @param execute * @param umpkey * @param <T> * @return */public static <T> T Execute (Execute execute, String umpkey) {System.out.println ("start========
        "+ Umpkey);
        try {return (T) Execute.doexecute ();
        catch (Exception e) {System.out.println ("umpkey===>" + Umpkey);

        } System.out.println ("end========" + Umpkey);
    return null;
    Private interface Execute {Object doexecute ();
        //test use of anonymous inner classes public static void main (string[] args) {Executehelper.execute (()-> study.print ());
        String xxx = Executehelper.execute (()-> study.print (), "xxx"); String Execute = Executehelper.execute (()-> StUdy.sayhello ("Zhangsan"), "zzz");
    System.out.println (xxx + "==========" + execute); public static class Study {public static String print () {System.out.println ("Hello world*****
            **");
        return "China"; public static string SayHello (String userName) {System.out.println ("Say Hello *******" + userName)
            ;
        Return "Descendants of the Dragon";
 }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.