Anonymous inner class explained

A cool guy who looks a little troublesome, but, it's still cool


/*
The advantage of anonymous inner class: one less class is defined
Disadvantage: cannot be reused
*/


public class fuck13{ //Static method public static void t(a a1){ a1.logout(); } public static void main(String[ ] args){ //t.(new b()); //Here we want to call the t method, here we use polymorphism //a a1=new b(), a new type b is created, which is automatically converted to type a //So it can be used as the actual parameter of the t method //The way of using an anonymous inner class //The whole new a(){```} is an anonymous inner class t(new a(){ public void logout(){// You can only inherit and then rewrite   System.out.println("The program has exited safely");} } ); } } //Define an interface, the interface cannot create objects interface a{ void logout(); } /*class b implements a{ public void logout(){//Can only inherit and then rewrite System.out.println("The program has exited safely"); }








































}*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325891192&siteId=291194637