java object oriented -- inner class

1. internal class

A: Internal class overview: define the class inside other classes, and this class is called internal class.
For example: Class B is defined in class A, and class B is the internal class.
B: Internal class access features
a: Internal classes have direct access to members of external classes, including private.
b: External classes to access members of internal classes, you must create objects.

public class MyTest {
    public static void main(String[] args) {
        //Inner class: define one class A into another class B, we call class A inner class, we can call class B outer class
        //According to the location of internal class definition, it can be divided into member internal class and local internal class
        //Member inner class: define inner class to member location
        //Local inner class: defines an inner class into a method of an outer class
    }
}

class B{
    //Member location: out of method in class
    class A {
       //Member inner class

    }

    public void show(){
        class C{
            //Local inner class
        }
    }


}

2. Internal class classification and direct use of internal classes of members

  • A: Classification by internal class location
    Member location: a class defined in the member location, known as a member internal class.
    Local location: a class defined in a local location is called a local internal class.
  • B: Member inner class
    How to directly access the members of an inner class in a test class.
    Format: external class name. Internal class name object name = external class object. Internal class object;

3. Common modifiers and application of member inner class

  • A: Modifier for member inner class:
    private to ensure data security
    static for easy access to data
    matters needing attention:
    a: External class data accessed by static internal classes must be statically decorated.
    b: Member methods can be static or non static
  • B: The access method of static decorated member inner class is:
    Format: external class name. Internal class name object name = new external class name. Internal class name ();
public class MyTest {
    public static void main(String[] args) {
        //I want to access members of inner classes
        //Create an object of an inner class
      Outer.Inner inner= new Outer().new Inner();
      inner.neiShow();
      System.out.println(inner.bb);
      //Features of inner class: it can directly access members of outer class, including private
        //If an external class wants to access the members of an internal class, it needs to create an internal class object
        Outer outer = new Outer();
        outer.test();
    }
}

class Outer{
    int num=20;
    private int aa=100;
    //Define member inner class
    class Inner{
        int bb=20;
        public void neiShow(){
            System.out.println("This is internal show Method");
            System.out.println(num);
            System.out.println(aa);
            waiShow();
            waiTest();
        }

        public void neiTest(){
            System.out.println("This is internal test Method");
        }
    }


    public void waiShow(){
        System.out.println("This is external show Method");
    }

    private void waiTest(){
        System.out.println("This is an external class private method");
    }

    public void test(){
        //Create an object of an inner class
        Inner inner = new Inner();
        System.out.println(inner.bb);
        inner.neiTest();
    }
}

4. Interview questions of members

Requirement: output 30, 20, 10 in the console with known variables.

class Outer {
		public int num = 10;
		class Inner {
			public int num = 20;
			public void show() {
				int num = 30;
				System.out.println(num);//30
				System.out.println(this.num);//20
				System.out.println(new Outer().num); //10
			}
		}
	}
	class InnerClassTest {
		public static void main(String[] args) {
			Outer.Inner oi = new Outer().new Inner();
			oi.show();
		}	
	}

5. The problem of local internal class accessing local variables

  • A: Members of external classes can be accessed directly
  • B: You can create internal class objects and call internal class methods through objects to use local internal class functions
  • C: Local inner class must be decorated with final to access local variables
    Why?
    Because the local variable will disappear with the completion of the method call, at this time, the local object does not immediately disappear from the heap memory, but also use that variable.
    In order to make the data continue to be used, it is decorated with fianl. In this way, what is stored in the heap memory is actually a constant value.
    After JDK 1.8, final will add by default. You don't need to add manually, but you need to know

6. Anonymous inner class

  • A: Anonymous inner class: it is the simplified writing method of local inner class.
  • B: Premise: there is a class or interface; the class here can be concrete or abstract.
  • C: format:
    new class name or interface name (){
    Rewrite method;
    } ;
  • D: What is the essence?
    It is a subclass anonymous object that inherits the class or implements the interface.

7. Method call of anonymous inner class

Method call of anonymous inner class

public class MyTest {
    public static void main(String[] args) {
        //Anonymous inner class: it is a shorthand form of local inner class
        //Anonymous inner class: in essence, it is an object whose object inherits the abstract class or implements the subclass object of the interface
        //Syntax of anonymous inner class
       /* new Abstract class name / interface name (){
            Overriding abstract methods in abstract classes / interfaces
        };
        */

        new AA() {
            @Override
            public void show() {
                System.out.println("Overriding the show Method 22222222222");
            }
        }.show();

    }

    public static void  set(AA aa){
        aa.show();
    }
}

abstract class AA{
    public abstract void show();
}

8. Application of anonymous inner class in development

First of all, we will review the case that the formal parameter of a method is a reference type,
We know that we need a subclass object here. Anonymous inner class is a subclass of anonymous object,
Therefore, you can use anonymous inner classes to improve the previous practice.

//The abstract class and interface can be written here
	public class MyTest3 {
    public static void main(String[] args) {
        new EE() {
            @Override
            public void hehe() {
                System.out.println("Ha-ha");
            }

            @Override
            public void haha() {
                System.out.println("Ha-ha");
            }
        }.haha();


        new EE() {
            @Override
            public void hehe() {
                System.out.println("Ha ha 222");
            }

            @Override
            public void haha() {
                System.out.println("Ha ha 2222");
            }
        }.hehe();

        //Name an object in a polymorphic way
        EE ee = new EE() {
            @Override
            public void hehe() {
                System.out.println("Ha ha 222333");
            }

            @Override
            public void haha() {
                System.out.println("Ha ha 22223333");
            }
        };
        ee.haha();
        ee.hehe();

    }
}


abstract class EE {
    public abstract void hehe();

    public abstract void haha();
}

9. this keyword in anonymous inner class

	 interface Inter {
		public static final int a = 23 ;
	}
	
	public class Test {

		public static void main(String[] args) {
			new Inter() {
				public void show() {
					//this anonymous inner class
					System.out.println(this.a);//23
					System.out.println(Inter.a);//23
		
				}
			}.show();
		}
	}

10. interview questions of anonymous internal class

Require "HelloWorld" output on console

interface Inter {
    void show();
}

class Outer {
    //Complement code
    public static Inter method() {
        return new Inter() {
            @Override
            public void show() {
                System.out.println("HelloWorld");
            }
        };
    }

}

class OuterDemo {
    public static void main(String[] args) {
        Outer.method().show();
    }
}
46 original articles published, praised 1, 776 visitors
Private letter follow

Tags: JDK

Posted on Wed, 29 Jan 2020 04:46:30 -0800 by RandomEngy