Beginners learn the inner classes of Java

  • 2021-10-24 23:10:28
  • OfStack

Directory Java Internal Class Member Internal Class Static Internal Class Local Internal Class Summary

Inner classes of Java

Concepts:

Internal class is to define a class inside a class. For example, if an B class is defined in A class, then B class is called internal class relative to A class, while A class is external class relative to B class. Member inner class Static inner class Local inner class Anonymous inner class

Features:

Internal classes can generate independent bytecode files after compilation. Internal classes can directly access private members of external classes without breaking the encapsulation. Internal classes can provide necessary internal functional components for external classes. To access the members of the inner class, the outer class must create the objects of the inner class

Member inner class

Class at the same level as instance variables and instance methods.

A member inner class cannot contain static members, but can contain static constants.


package com.cnblogs;
// This class is used to implement the 
public class Application {
	public static void main(String[] args) {
		Outer outer = new Outer();// Instantiation    External class 
		Outer.Inner inner = outer.new Inner();// Instantiation    Inner class 
        //1 Step-by-step instantiation of internal classes 
        //Inner inner = new Outer().new Inner();
		// Properties of external classes 
		System.out.println(outer.age);//21
		// External class method 
		outer.out1();// This is the method of the external class 
		// Internal class attributes 
		System.out.println(inner.in);// Internal class attributes 
		// Internal class method 
		inner.inner();// This is an inner class method 
		// The inner class gets the private properties of the outer class 
		inner.getName();// Gets the private property of the external class: 
		// The inner class gets the private method of the outer class 
		inner.getMethod();// This is the private method of the external class 
	}
}

package com.cnblogs;
/*
 *  This class is used to test that internal classes can directly access private members of external classes 
 */
public class Outer {
	// Private properties of external classes 
	private String name = " Qi ";
	// Properties of external classes 
	public int age = 21;
	// Private methods of external classes 
	private void out() {
		System.out.println(" This is the private method of the external class ");
	}
	// Methods of external classes 
	public void out1() {
		System.out.println(" This is the method of the external class ");
	}
	 class Inner{
		// Internal class attributes 
		public String in = " Internal class attributes ";
		// Internal class method 
		public void inner() {
			System.out.println(" This is an inner class method ");
		} 
		// Gets the private properties of an external class 
		public void getName() {
			System.out.println(" Gets the private properties of the external class: " + name);
		}
		// Gets the private method of an external class 
		public void getMethod() {
			out();
		}
	}
}

Note: When the external class and the internal class have the same name attribute, the internal class attribute will be accessed first


package com.cnblogs;
import com.cnblogs.Outer.Inner;
/*
 *  This class is used to implement the 
 */
public class Application {
	public static void main(String[] args) {
		Inner inner = new Outer().new Inner();
		inner.show();
	}
}

package com.cnblogs;
/*
 *  This class is used to test the properties of inner and outer classes with the same name 
 */
public class Outer {
	// External class attributes 
	private String name = " Zhang 3";
	class Inner{
		// Internal class attributes  
		private  String name = " Qi ";
		// Internal class method , Used for printing 
		public void show() {
			// What is printed here is the attribute of the inner class: Qi 
			System.out.println(name);
			// This is the property of printing external classes: Zhang 3
			System.out.println(Outer.this.name);
		}
	}
}

Static inner class

Does not depend on external class objects, can be created directly or accessed by class name, and can declare static members


package com.cnblogs;
import com.cnblogs.Outer2.Inner;
/*
 *  This class is used to implement the 
 */
public class Application {
	public static void main(String[] args) {
		// Create inner class objects directly 
		Inner inner = new Outer2.Inner();
		// Invoke method 
		inner.show();
	}
}

package com.cnblogs;
/*
 *  This class is used for velocity measurement static internal class 
 */
// External class 
public class Outer2 {
	private String name = " Qi ";
	private int age = 21;
	// Static inner class   Usage and external classes 1 Sample 
	static class Inner{
		private String in = " Static inner class attributes ";
		private int inner = 99;
		// Static member 
		private static int inners = 999;
		// Invoke method 
		public void show() {
			// Invoking external class properties , Create an external class object first 
			Outer2 outer = new Outer2();
			// Then call the external class property 
			System.out.println(outer.name);// Qi 
			System.out.println(outer.age);//21
			// Invoking static inner class properties 
			System.out.println(in);// Static inner class attributes 
			System.out.println(inner);//99
			// Calling static members of static inner classes 
			System.out.println(inners);//999
			System.out.println(Inner.inners);//999
		}
	}
}

Local inner class

Defined in an external class method, the scope and scope of creating objects are limited to the current method.

When a local inner class accesses a local variable in the current method of an external class, the variable must be modified as final because there is no guarantee that the life cycle of the variable is the same as itself.

Limit the scope of use of classes.


package com.cnblogs;
// This class is used to implement the 
public class Application {
	public static void main(String[] args) {
		Outer3 outer = new Outer3();
		outer.show();
	}
}

package com.cnblogs;
/*
 *  This class is used to test local inner classes 
 */
// External class 
public class Outer3 {
	private String name = " Qi ";
	private int age = 21;
	public void show() {
		// Define local variables 
		String in = " Suzhou ";
		// Local inner class : You cannot add any access modifiers 
		class Inner{
			// Attributes of local inner classes 
			private String phone = "10086";
			// Local inner class method 
			public void show2() {
				// Access external class properties 
				System.out.println(name);//Outer3.this.name
				System.out.println(age);
				// Access internal class properties 
				System.out.println(phone);//this.phone
				// Access to local variables :jdk1.7 Requirements   Variable must be constant final , jdk1.8 Automatically add final
				System.out.println(in);
			}
		}
		// Create an internal class object 
		Inner inner = new Inner();
		inner.show2();
	}
}

Anonymous inner class

Local inner class without class name (1 cut characteristics are the same as inner class).

Must inherit 1 parent class or implement 1 interface.

The syntax of defining class, implementing class and creating object can only create 1 object of this class.

Advantages: Reduced code Disadvantages: Poor readability

package com.cnblogs;
/*
 *  This class is used to implement local inner classes 
 */
public class TestUsb {
	public static void main(String[] args) {
		// Create variables of interface type 
//		Usb usb = new Mouse();
//		usb.service();
		// Local inner class 
//		class Fan implements Usb{
//
//			@Override
//			public void service() {
//				System.out.println(" The link is successful, you can use it! ! ! ! ");
//				
//			}
//			
//		}
		// Creating Objects Using Local Inner Classes 
//		Usb usb = new Fan();
//		usb.service();
		// Use anonymous inner class optimization (equivalent to creating a 1 Local inner classes) 
		Usb usb = new Usb() {
			@Override
			public void service() {
				System.out.println(" The link is successful, you can use it! ! ! ! ");
			}
		};
		usb.service();
	}
}

package com.cnblogs;
// Interface 
public interface Usb {
	// Services 
	void service();
}

package com.cnblogs;
/*
 *  This class is used to test that internal classes can directly access private members of external classes 
 */
public class Outer {
	// Private properties of external classes 
	private String name = " Qi ";
	// Properties of external classes 
	public int age = 21;
	// Private methods of external classes 
	private void out() {
		System.out.println(" This is the private method of the external class ");
	}
	// Methods of external classes 
	public void out1() {
		System.out.println(" This is the method of the external class ");
	}
	 class Inner{
		// Internal class attributes 
		public String in = " Internal class attributes ";
		// Internal class method 
		public void inner() {
			System.out.println(" This is an inner class method ");
		} 
		// Gets the private properties of an external class 
		public void getName() {
			System.out.println(" Gets the private properties of the external class: " + name);
		}
		// Gets the private method of an external class 
		public void getMethod() {
			out();
		}
	}
}
0

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: