Why use method local abstract inner classes

后端 未结 10 1765
名媛妹妹
名媛妹妹 2021-02-05 04:45

One of the legal modifiers you can use with method local inner classes is abstract.

For example:

public class Outer {
    public void method(){
        a         


        
10条回答
  •  孤街浪徒
    2021-02-05 05:12

    package dto;
    
    public class Outer {
    
        public void method(int x, int y){
            abstract class Inner{
                abstract void performAction(int x,int y);
            }
            class InnnerA extends Inner{
    
                @Override
                void performAction(int x,int y) {
                    int z =x+y;
                    System.out.println("addition :" + z);
    
                }
    
            }
            class InnnerB extends Inner{
    
                @Override
                void performAction(int x,int y) {
                    System.out.println("multiply :"+x*y);
    
                }
    
            }
            Inner inner1 = new InnnerA();
            inner1.performAction(x,y);
            Inner inner2 = new InnnerB();
            inner2.performAction(x,y);
        }
        public static void main(String args[]){
            Outer outer = new Outer();
            outer.method(10,20);
        }
    }
    

    You can use it like this.

提交回复
热议问题