JAVA Study Notes (46)-internal class details, java Study Notes
Member internal class
/** Internal Class * is defined as a Class in another Class. It is called an internal Class. The Inner Class * contains internal classes and is called an external Class Outer Class ** Application Scenario: event processing in the form program ** category: * member internal class * local internal class * static internal class * anonymous internal class ** member internal class * 1. access the internal class in the external class and access all the members in the internal class, including the private modified * 2. when accessing an internal class outside the external class, you cannot access the private modified member in the internal class * 3. the internal class accesses the external class directly. If the internal class and the external class have the same variable name, you can use the "external class. this. variable "*/public class Test12 {public static void main (String [] args) {Outer out = new Outer (); // create an external class Object/* out. print (); // call the external Class Method * // 2. access the internal class outside the external class // create the object of the internal class outside the external class, Syntax: External class. internal Class Object Name = External class object. new internal class (); // Outer. inner in = out. new Inner (); // The object Inner in = out of the external class must be created first. new Inner (); // System. out. println (in. age); // The private member in. show () ;}}/** external class Outer. After the internal class Inner * is compiled, three bytecode files are generated: Test12.class and Outer. class, Outer $ Inner. class * member internal class name: External class name $ internal class name. class */class Outer {// external class private String name = "tom"; // static String address = "Nanjing "; class Inner {// internal class private int age = 20; // The member variable private String name = "Zhang San"; String address = "Beijing"; public void show () in the internal class () {String address = "Tianjin"; System. out. println ("show method in internal class"); // 3. access the external Class System in the internal class. out. println ("name in the internal class:" + this. name); System. out. println ("name in external class:" + Outer. this. name); System. out. println ("age in internal class:" + age); System. out. println ("address in the internal class:" + this. address); System. out. println ("address:" + Outer. address); System. out. println ("internal class local variable address:" + address) ;}}// 1. access the internal class public void print () {System. out. println ("print method in external class"); Inner in = new Inner (); // directly create an internal Class Object System. out. println (in. age); // access the attributes of the internal class and access the private member in. show ();}}
Local internal class
/** Local internal class * is also called the internal class of the method, that is, class * 1 in the method that defines the external class. access scope: it can only be used in external class methods, but cannot be modified using modifiers * 2. A non-final modified member */public class Test01 {public static void main (String [] args) cannot be accessed in a local internal class) {MOuter out = new MOuter (); out. show () ;}} class MOuter {String name = "tom"; public void show () {final int age = 20; System. out. println ("show method in external class"); class MInner {// local internal class String sex = "male"; private String address = "Nanjing"; public void print () {System. out. println ("print method in local internal class"); System. out. println ("name of the member variable in the external class:" + name); System. out. println ("local variable age in external class:" + age) ;}// only MInner in = new MInner (); System can be used in external class methods. out. println (in. sex); System. out. println (in. address); in. print ();}}
Static internal class
/** Static internal class * The internal class of the member modified using static cannot access non-static members of the external class */public class Test02 {public static void main (String [] args) {// create a static internal class. You do not need to create the object SInner in = new SInner (); in. show () ;}} class SOuter {static String name = "tom"; static class SInner {// static internal class String sex = "male"; public void show () {System. out. println ("show method in static internal class"); System. out. println ("name in external class:" + name );}}}
Anonymous internal class
/** Anonymous internal class * is an internal class without a name. Because this class only needs to be used once, it is not necessary to start a name * 1. when creating an anonymous internal class, it must be declared as part of the new statement. Usage: * interface | abstract class Object Name = new interface | abstract class {* // anonymous internal class *} * 2. when creating an anonymous internal class, an anonymous internal class object is automatically created and assigned to the variable */public class Test03 {public static void main (String [] args) {Service s = new Service () {// anonymous internal class // abstract method public void show () {System. out. println ("anonymous internal class implementing Service interfaces") ;}}; s. show (); Animal pig = new Animal () {@ Override public void play (){ System. out. println ("pig run .... ") ;}@ Override public void show () {System. out. println (" pig Eat ..... ") ;}}; Pig. show (); pig. play (); Student stu = new Student () {public void show () {System. out. println ("I Am a primary school student, hee") ;}}; stu. show () ;}/ ** define interface */interface Service {// abstract method void show ();}/* abstract class */abstract class Animal {String name; public void show () {System. out. println ("animals are eating .... ");} Public abstract void play ();} class Student {public void show () {System. out. println (" ");}}