Internal class, application embodiment in actual development, development embodiment

Source: Internet
Author: User

Internal class, application embodiment in actual development, development embodiment

Definition:The class placed inside a class is called an internal class.

In Java, a class can be defined in another class or a method. Such a class is called an internal class. The internal class is still an independent class. After compilation, the internal class will be compiled into an independent. class file, but the class name and $ symbol of the external class are prefixed.

An internal class is a concept during compilation. Once compiled successfully, it will become completely different. For an external class named outer and its internal defined internal class named inner. After compilation, the outer. class and outer $ inner. class classes are displayed. Therefore, the member variable/Method Name of the internal class can be the same as that of the external class.

Internal classes can be static or public, default, protected, or private. (For external top-level classes with the same class name and file name, only public and default classes can be used.

Category:1. Member internal class 2. static internal class 3. Anonymous internal Class 4. Method internal class (almost unnecessary)

Purpose:Indirectly solves the problem of multi-inheritance (an internal class inherits a class, and an external class inherits a class to implement multi-inheritance.The most important role);

Internal class has peripheral classAll elements;

 

The internal class of a member, as its name implies, defines a class within a class. As a Member attribute (member field) of the class, the internal class of the class can access the attributes and methods of the class, because it is also a member of this class.

Note that the internal class of a member cannot contain static variables and methods.Because the internal class of a member must first create an external class to create its ownTo understand this point, you can understand more things and omit more details here.

Code:

  

Package internal class. Member internal class; public class Person {public void sayPerson () {System. out. println ("hello Person ");}}

  

Package internal class. internal class of the member; public class Student extends Person {private int age; public int getAge () {return age;} public void say () {System. out. println ("hello Student");} private Grade1 getGrade1 () {return new Grade1 (); // It is recommended to instantiate} private class Grade1 {public void say () {Student. this. sayPerson (); // Student of the external class called by the internal class. this. say (); System. out. println (Student. this. getAge (); System. out. println ("hello Grade1") ;}} public static void main (String [] args) {Student stu = new Student (); Student. grade1 grade1 = stu. getGrade1 (); // Student. grade1 grade2 = stu. new Grade1 (); two ways to instantiate internal classes grade1.say ();}}

 

Static internal class: as the name suggests, internal classes are static. The advantage is that internal classes can be instantiated without getting external class objects. However, internal classes cannot reference external classes.NonStatic members (no one is perfect)

Example code: Entry in HashMap

Package internal class. static internal class; public class Test {public int age = 1; public static String name = "shaolin"; static class Baby {// static internal class public void say () {// System. out. println (age); you cannot access the age attribute. You can only access static attributes (easy to understand) System. out. println ("hello" + name) ;}} public static void main (String [] args) {Test. baby temp = new Test. baby (); // instantiate the static internal class temp. say ();}}

  

 

Anonymous internal class, as its name implies, is an internal class without a name

Code on

Package internal class. anonymous internal class; public class Test {public static void main (String [] args) {// inherits the Mother class and implements the abstract method "Person person = new Person () {@ Overridepublic void say () {System. out. println ("hello, anonymous internal class") ;}}; person. say () ;}} abstract class Person {// it can also be an interface's public abstract void say ();}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.