Inner class

Time:2020-9-26

Anonymous class

Static decorated members and access

package com.ice.test00;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Animal {
    //Member variable
    int age;
    String name;

    //Member method
    public void eat() {
        System.out.println("eat...");
    }

    /**
     *Variables modified by static: static variables (class variables)
     *Methods modified by static: static methods (class methods)
     *Static variables and methods are called static members
     *Static members are accessed by "class name." "access (standard) or" object. "(nonstandard, not recommended)
     * 
     *This and super cannot be used in static decorated methods or code blocks
     *Static cannot modify constructors
     */
    //Static variable
    static char sex;
    static int foot;

    //Static method
    public static void sleep() {
        System.out.println("sleep...");
    }

    public static void main(String[] args) {
        //Access static members through class name
        System.out.println(Animal.sex);
        System.out.println(Animal.foot);
        Animal.sleep();

        //Accessing static members through object. Is nonstandard and is not recommended
        Animal a = new Animal();
        System.out.println(a.sex);
        System.out.println(a.foot);
        a.sleep();
    }
}

Static application

package com.ice.test;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class People {
    private String name;
    private int age;
    private static String city;

    public People() {
    }

    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", city='" + city + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}
package com.ice.test;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Test02 {
    public static void main(String[] args) {

        People p1 = new People("max", 23);
        P1. Setcity ("Beijing");
        System.out.println(p1);

        People P2 = new people ("Xiaolong", 21);
        P2. Setcity ("Guiyang");
        System.out.println(p2);

        People P3 = new people ("Xiaomin", 22);
        System.out.println(p3);
    }

}

Static initialization block

package com.ice.test;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Count {
    public int number;

    Private static int count; // used for counting

    //Used for initial static variables, prior to constructor execution
    static {
        System.out.println ("executed and executed once when class loading");
        count = 1;
    }

    public static int getCount() {
        return count;
    }

    public Count() {
        count++;
        number = count;
        System.out.println (the "constructor is executed");
    }
}
package com.ice.test;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Test01 {
    public static void main(String[] args) {
        Count c = new Count();
        new Count();
        System.out.println(c.number);
    }
}

Implementation results:

Class loaded and executed once
The constructor is executed
The constructor is executed
2

Singleton design pattern

Sluggard

package com.ice.test01;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
//Sluggard
public class SingleTon {
    public double r = Math.random();
    private static SingleTon single = null;

    //Privatize the constructor to prevent other classes from creating objects
    private SingleTon() {

    }

    //Static factory: an instance specifically used to produce classes
    public static SingleTon getInstance() {
        if (single == null) {
            single = new SingleTon();
        }
        return single;
    }

}

Hungry Han style

package com.ice.test02;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 *Inner class: a class defined in a class or method
 *The inner class is actually a class, which can have member variables and member methods
 *Classification: member inner class, static inner class, local inner class, anonymous inner class
 */
//Hungry Han style
public class SingleTon {
    public double r = Math.random();
    private static SingleTon single = new SingleTon();

    //Privatize the constructor to prevent other classes from creating objects
    private SingleTon() {

    }

    //Static factory: an instance specifically used to produce classes
    public static SingleTon getInstance() {
        return single;
    }

}
package com.ice.test02;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Test {

    public static void main(String[] args) {
        SingleTon single1 = SingleTon.getInstance();
        System.out.println(single1.r);
        SingleTon single2 = SingleTon.getInstance();
        System.out.println(single2.r);
    }
}

Definition of inner class

public abstract class Animal {
    public abstract void eat();
}

public interface Inf1 {
    //Start
    public abstract void start();
}

public interface Inf2 {
    //Start
    public abstract void start();
}

public abstract class Other {
    public abstract void othereat();
}

package com.ice.test03;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
//Outer class
public class Outerclass extends Other implements Inf1 {
    public static int a = 0;

    @Override
    public void start() {//Start

    }

    @Override
    public void othereat() {

    }

    //Inner class
    class InnerClass {
    }

    //Inner class实现接口
    class InnerClassInf implements Inf2 {

        @Override
        public void start() {//Start

        }
    }

    //Inner class继承抽象类
    class InnerClassAbstract extends Animal {

        @Override
        public void eat() {

        }
    }

}

The content of the class

package com.ice.test04;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public abstract class OuterClass {
    Int a; // member variable
    Static int b; // static variable
    Final static int c = 10; // constant

    {
        //Initialization block
    }

    static {
        //Static initialization block
    }

    public OuterClass() {
        //Constructors

    }

    public void fun1() {
        //Member method

    }

    public static void fun2() {
        //Static member method

    }

    Public abstract void fun3(); // abstract method

    class InnerClass {
        //Inner class
        
    }

}

Member inner class

package com.ice.test05;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class OuterClass {
    Int a = 10; // member variable
    Static int b = 20; // static variable

    /**
     *Access member inner class in external class: new inner class name (). Inner class object
     */
    //Member method
    public void fun() {
        System.out.println(new InnerClass().x);
    }

    //Member inner class
    class InnerClass {
        int x = 10;

        //Static int y = 20; // static members cannot exist in inner classes

        /**
         *Access external class in member inner class: external class name. This. External class object
         *This represents the current inner class object in the inner class
         */
        public void fun() {
            System.out.println(OuterClass.this.a);
            System.out.println(this.x);
        }
    }
}

Static inner class

package com.ice.test06;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
//Outer class
public class OuterClass {

    //Static inner class
    public static class InnerClass {
        public static int a = 10;

        public static void fun() {
            System.out.println("statie fun...");
        }
    }

    public void test() {
        System.out.println(InnerClass.a);
    }
}
package com.ice.test006;

import com.ice.test06.OuterClass;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Test {
    public void m() {
        System.out.println(OuterClass.InnerClass.a);
    }
}

Local inner class

package com.ice.test07;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class OuterClass {
    public int a = 10;

    public void fun() {
        //Local variable
        final int x = 100;
        /**
         *Local inner class: defined inside a method or at parameter location
         *Scope is only valid within this method, and permission modifiers cannot be used
         */
        class InnerClass {
            public void test() {
                System.out.println ( OuterClass.this . a); // access external class members through "external class name. This. External object"

                System.out.println (x) ; // access the local variable of the method, but this local variable must be decorated with final
            }
        }
    }
}

Anonymous Inner Class

package com.ice.test08;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Animal {
    public void eat() {
        System.out.println("Animal...");
    }
}
package com.ice.test08;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 *Anonymous inner class:
 *1. Create when the member variable is assigned
 *2. Define and use directly within the method
 *3. Create in method parameter position
 */
//Outer class
public class NoNameClass {
    Animal a = null; // use the default value of null when declaring a reference object
    Animal A2 = new animal(); // the object is created when the reference variable is declared

    /**
     *1. Create when the member variable is assigned
     */
    //Declare the reference variable and create the anonymous inner class object new class name () {}--- > anonymous inner class object
    Animal a3 = new Animal() {
        @Override
        public void eat() {
            super.eat();
        }
    };

    /**
     *2. Define and use directly within the method
     */

    public void fun() {
        //Anonymous inner class -- > anonymous object. It will be recycled once it is used up
        new Animal() {
        };
        Animal a = new Animal() {
            @Override
            public void eat() {
                System.out.println (eat method of anonymous inner class);
            }
        };
        a.eat();
    }

    /**
     *3. Create in method parameter position
     *
     * @param a
     */
    public void m(Animal a) {

    }

    public static void main(String[] args) {
        New nonameclass(). M (New animal() {// created at the method parameter position

        });
    }
}
//General class
public class Animal {
    public void sleep() {

    }
}

//Abstract class
public abstract class Computer {

    public abstract void play();
}

//Interface
public interface Mouse {
    public abstract void click();
}

package com.ice.test09;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class NoNameClass {
    //General class实现匿名内部类
    Animal a = new Animal() {
        @Override
        public void sleep() {
            System.out.println ("sleep() method of anonymous inner class");
        }
    };
    //Abstract class实现匿名内部类:在匿名内部类中必须重写抽象方法
    Computer c = new Computer() {
        @Override
        public void play() {
            System.out.println ("play() method of anonymous inner class");
        }
    };

    //Interface实现匿名内部类:在匿名内部类中必须重写抽象方法
    Mouse m = new Mouse() {
        @Override
        public void click() {

        }
    };
}

Using anonymous inner classes as parameters

package com.ice.test10;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
//General class
public class Animal {
    public void sleep() {
        System.out.println("Animal sleep...");
    }
}

class Dog extends Animal {
    @Override
    public void sleep() {
        System.out.println("Dog sleep");
    }
}

package com.ice.test10;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
//Abstract class
public abstract class Computer {
    public abstract void play();
}

class MyComputer extends Computer {
    @Override
    public void play() {
        System.out.println("MyComputer play...");
    }
}

package com.ice.test10;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
//Interface
public interface Mouse {
    public abstract void click();
}

class Lenovo implements Mouse {
    @Override
    public void click() {
        System.out.println("Lenovo click...");
    }
}

package com.ice.test10;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class NoNameClass {
    /**
     *When the common inner class is used as a parameter, it can pass the common class object, subclass object and anonymous inner class object
     */
    public void m1(Animal a) {
        a.sleep();
    }

    /**
     *When the abstract class is used as a parameter: the subclass object can be passed, and the inner class object can be anonymous
     *
     * @param c
     */
    public void m2(Computer c) {
        c.play();
    }

    /**
     *When the parameter is used as an interface: the implementation class object can be passed, and the inner class object can be anonymous
     *
     * @param m
     */
    public void m3(Mouse m) {
        m.click();
    }
}

package com.ice.test10;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Test {
    public static void main(String[] args) {
        NoNameClass n = new NoNameClass();

        //Call a method whose parameter is a normal class
        n.m1(new Animal());//Animal a=new Animal();
        n. M1 (New dog()); // animal a = new dog(); // upward transformation
        n. M1 (New animal() {// animal a = anonymous inner class object
            @Override
            public void sleep() {
                System.out.println (common class implements anonymous inner class);
            }
        });

        //Call a method whose parameter is an abstract class
        n. M2 (New mycomputer()); // computer C = new mycomputer's upward transformation
        n.m2(new Computer() {
            @Override
            public void play() {
                System.out.println (abstract class implements anonymous inner class);
            }
        });

        //Call a method whose parameter is an interface
        n. M3 (New lenovo()); // mouse M = new lenove(); // upward transformation
        n.m3(new Mouse() {
            @Override
            public void click() {
                System.out.println ("interface implements anonymous inner class");
            }
        });
    }

}

task

1. Use of inner classes:
(1) Define an external class father with a member variable name and assign an initial value.
(2) define an internal class Child and define a getValue () method to invoke the name variable of external class Father in the method.
(3) Define a test class, create a child object in the main method of the test class, and call the getvalue() method

package com.ice.test11;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Father {
    private String name = "max";

    class Child {
        public void getValue() {
            System.out.println(Father.this.name);
        }
    }

    public void getValue() {
        Child c = new Child();
        c.getValue();
    }
}

package com.ice.test11;

/**
 * @author lucky_ice
 *Copyright:****
 *Version: version 1.0
 */
public class Test {
    public static void main(String[] args) {
        Father f = new Father();
        f.getValue();
    }
}