Inner class, lambda expression

Inner class:

Define a class in a class.

**Category: **Member Inner Class: In the member position of the class

​ Local inner class: in the local position of the class

Example: member inner class

public class Test1Inner {
    
    
    public static void main(String[] args) {
    
    
        /*
                创建内部类对象的格式:
                外部类名.内部类名 对象名 = new 外部类对象().new 内部类对象();
         */
        //访问内部类的成员
        Outer.Inner i = new Outer().new Inner();
        System.out.println(i.num);
        i.show();
    }
}
class Outer{
    
    

    int a = 10;

    class Inner{
    
    //成员内部类
        int num = 10;

        public void show(){
    
    
            System.out.println("Inner..show");
            //内部类去访问外部类成员,可以直接访问,包括私有
            //外部类要访问内部类成员,就必须创建对象
            System.out.println(a);
        }
    }
}

Member inner class

Overview:
Define the inner class outside the method in the class.

Private member internal class access (private): create object access in the external class where you are.

static:

Static member internal class access: external class name. internal class name object name = new external class name. internal class name();

Static methods in internal classes of static members: external class name. internal class name. method name();

public class Test2Innerclass {
    
    
    /*
        私有成员内部类演示
     */
    public static void main(String[] args) {
    
    
        //Outer.Inner oi = new Outer().new Inner();Inner被private私有化,访问不到
        Outer o = new Outer();
        o.method();
    }
}

class Outer{
    
    
    private class Inner{
    
    
        public void show(){
    
    
            System.out.println("inner..show");
        }
    }
    //就在自己里面,直接访问就OK
    public void method(){
    
    
        Inner i = new Inner();
        i.show();
    }
}
public class Test3Innerclass {
    
    
    /*
    静态成员内部类
     */
    public static void main(String[] args) {
    
    
        //对于静态成员内部类
        //外部类名.内部类名 对象名 = new 外部类名.内部类名();
        Outer.Inner oi = new Outer.Inner();
        oi.show();
        Outer.Inner.method();
    }
}
class Outer{
    
    
    static class Inner{
    
    
        public void show(){
    
    
            System.out.println("inner..show");
        }
        public static void method(){
    
    
            System.out.println("inner..method");
        }
    }
}

Local inner class:

Local inner classes are classes defined in methods , so they cannot be used directly by the outside world. Objects need to be created and used inside methods.

This class can directly access the members of the external class, and can also access the local variables in the method

effect:

​ The things inside the method will automatically disappear after the method is executed.
So, local inner classes can be used to "temporarily create subclass objects"

public class Test4InnerClass {
    
    
    /*  局部内部类
        编写位置:方法中
        访问方式:只能在方法中,创建对象并访问
     */
    public static void main(String[] args) {
    
    
        Outer o = new Outer();
        o.method();
    }

}
class Outer{
    
    
    public void method(){
    
    
        class Inner{
    
    
            public void show(){
    
    
                System.out.println("show...");
            }
        }
        Inner i = new Inner();
        i.show();
    }
}
Anonymous inner class: important

Essentially an object

Overview: An anonymous inner class is essentially a special local inner class (defined inside a method)

Prerequisite: there needs to be an interface or class

format:

new 类名或者接口名(){

          重写方法;

		};


example:

new Inter(){
  public void show(){  
  }
}

example:

public class Test5InnerClass {
    
    
    /*
    1.创建实现类,通过implements关键字去实现接口
    2.重写方法
    3.创建实现类对象
    4.调用重写后的方法
    前提:需要存在一个接口或类
     */
    public static void main(String[] args) {
    
    
       // InterImp ii = new InterImp();
        //ii.show();
        
        
        //将继承\实现,方法重写,创建对象放在一步写
        //实现了Inter接口的,一个实现类对象
        new Inter(){
    
    //为什么要放在这里面?用完就销毁了
            @Override
            public void show() {
    
    
                System.out.println("匿名内部类中的show方法");
            }
        }.show();
        
        
        Inter1 i = new Inter1(){
    
    

            @Override
            public void show1() {
    
    
                System.out.println("show1");
            }

            @Override
            public void show2() {
    
    
                System.out.println("show2");
            }
        };
        i.show1();
        i.show2();
    }
}
interface Inter{
    
    
    void show();
}
interface Inter1{
    
    
    void show1();
    void show2();
}
//class InterImp implements Inter{
    
    
//
//    @Override
//    public void show() {
    
    
//
//        System.out.println("InterImp 重写的show方法");
//    }
//}

The method parameter is an interface

Lambda expression:

Less code, more clear focus

Example: optimized for anonymous inner classes

public class Test {
    
    
    public static void main(String[] args) {
    
    
        /*playGame(new Game() {
            @Override
            public void play () {
                System.out.println("DNF");
            }
        });*/
         /* //lambda表达式的写法
        //原则:能推断就省略

        playGame(() ->{ System.out.println("DNF"); });
        //一定是传的是"Game"接口的子类对象
        //一定实现了play方法*/
        playGame(() ->{
    
    
                System.out.println("DNF");
        });


    }

    //玩游戏的方法
        public static void playGame(Game g){
    
    
            g.play();
        }


}
interface Game{
    
    
    public abstract void play();
}

Lambda expression format:
format: (formal parameter)-> code block format: (formal parameter) -> (code block)Grid style:( Form Formula parameter number ) ->Generation of the code block
Lambda expressions actually "anonymous inner classes" essentially simplified wording.
(): Is, "the method of rewriting," the parameter.
{}: Is, "the method of rewriting" method thereof.

Formal parameters: if there are multiple parameters, separate the parameters with commas

Use premise: "There is an interface, and there is only one abstract method in the interface

example:

public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        //匿名内部类
          useStringHandler(new StringHandler() {
    
    
              @Override
              public void printMessage(String mes) {
    
    
                  System.out.println("dks"+mes);
              }
          });
          //lambda
          useStringHandler((String mes) ->{
    
    
            System.out.println("lambda"+mes);
        });
    }
    public static void useStringHandler(StringHandler stringHandler){
    
    
        stringHandler.printMessage("0000");
    }
}
interface StringHandler{
    
    
    void printMessage(String mes);
}

Omission rules:

1. The parameter type can be omitted, but if there are multiple parameters, only one cannot be omitted

2. If there is only one parameter, the parentheses can be omitted

3. If there is only one statement in the code block, you can omit the braces and semicolons, or even return

Guess you like

Origin blog.csdn.net/qq_42073385/article/details/108158011
Recommended