In java learning, internal Member classes, anonymous internal classes (small records in java learning), and java Anonymous

Source: Internet
Author: User

In java learning, internal Member classes, anonymous internal classes (small records in java learning), and java Anonymous

In java learning, member Internal classes and anonymous internal classes (small records in java learning)Author: Star)

 

Internal class:

Define another class in a class. Such a class is called an internal class.

File Name of the internal class: External class name $ internal class name. class

 

There are two internal classes:

1. Member internal class

How to access member Internal classes:

Method 1: Define a method in the external class to create an internal class object and then access it through the object.

Method 2: You can directly create an internal Class Object in other classes and access the object through the object. Vertices.

Format: External class name. Internal class name object name = new external Class Name (). new internal class name ();

Precautions for using internal classes:

1. If the external class and internal class have member variables of the same name, the Java Virtual Machine in the internal class accesses the member variables in the internal class by default. You can use the format: External class name. this. variable name.

2. If the Members in the internal class are modified by private, you can only use method 1 to access the members of the Internal class.

3. If any variable in the internal class is modified by static, the class must also be a static class.

Benefits of using internal classes:

An internal class can directly access all members of an external class. It is actually called by external classes. External classes have internal classes.

When to use internal classes:

When describing an object A, we find that another object B is required in object A. object B is very complicated, and object B wants to access data in object. In this case, it is more convenient to use internal classes to describe B objects.

For example, people have heart and blood. A person is a class, a heart is a class, and blood is also a class. Therefore, it is very troublesome for the heart to access the blood.

1 package study; 2 3 class Outer {4 5 int I = 10; // built-in member variable external class 6 7 // define an internal class 8 // member Internal class 9 class Inner {10 11 int I = 20; // The internal class variable 12 13 public void print () {14 System. out. println ("this is an internal class" + I); // print the 2015 System. out. println ("this is an internal class" + Outer. this. i); // access the attributes of the external Class 16} 17} 18 // create a method to create an internal Class Object (method 1) 19/* public void instantce () {20 // create an internal Class Object 21 Inner inner = new Inner (); 22 inner. print (); 23} */24} 25 // other classes 26 public class star {27 public static void main (String [] args) {28/* // access the internal class in other classes (method 1) 29 Outer outer = new Outer (); 30 outer. instantce (); */31 32 // direct access to other classes: (method 2) 33 Outer. inner inner = new Outer (). new Inner (); 34 inner. print (); 35 36} 37}

2. Local internal class

Local internal class: the class defined in the class method. Such a class is called a local internal class.

Local internal classes can only be accessed in the first way.

Notes for using local internal classes:

If the local internal class accesses the local variables in the method, the local internal class in the method must be modified using final.

1 class Outer {2 String name = "external class name"; 3 int I = 20; 4 5 public void test () {6 7 final int I = 30; // This method disappears when local variables exit. 8 // Why is final used for modification? 9/* after the test method is executed, the variable I disappears from the memory, while the inner object does not disappear after the method is executed, using I here is like delaying the lifecycle of I, which is inconsistent with the definition of local variables. Therefore, the final modifier is equivalent to setting a replica of I to the test method, and the actual I has disappeared. 11 **/12 13 // local internal class 14 class Inner {15 public void print () {16/* int I = 10; after cancellation, the study obtained 30 I */17 System. out. println ("this is a local internal class method" + I); // print 1018} 19} 20 // create a local object 21 Inner inner = new Inner (); // after the test is completed, this object may still exist (it is determined by the Java Virtual Machine and does not know when). If it exists, it is equivalent to a delay in the lifecycle of I. 22 inner. print (); 23} 24} 25 26 public class Star {27 28 public static void main (String [] args) {29 // access the local internal class 30 Outer out = new Outer (); 31 out in other classes. test (); 32 33 // The following statements are incorrect: 34/* Outer. inner inner = new Outer (). new Inner (); 35 inner. print (); */36} 37}

Anonymous internal class (class without class name)

Benefits: Simplified writing

Prerequisites for anonymous internal classes:External classes must have inheritance or implementation relationships.

Because it has no name, it is actually created by the parent class, expressed in polymorphism. Therefore, the relationship must be inherited or implemented.

Its implementation is actually expressed in the form of polymorphism, so the above conditions must be met.

Anonymous internal classes are generally used to pass actual parameters. (Same as anonymous object usage)

Abstract class Animal {public abstract void run (); public abstract void sleep ();} // external class // create a Dog internal class Outer in Outer {// do not need to be anonymous/* class Dog extends Animal {public void run () {System. out. println ("Running Dog");} public void sleep () {System. out. println ("sleeping Dog") ;}} */public void print () {// Dog d = new Dog (); // d. run (); // anonymous Dog (created through the parent class) // anonymous internal class: only no name, other and common classes are no different. Animal a = new Animal () {// It is not the parent class to create an object. Anonymous internal class (subclass) // the specific implementation of public void run () {System. out. println ("Running Dog") ;}; public void sleep () {System. out. println ("sleeping dog") ;}};. run ();. sleep () ;};} public class Star {public static void main (String [] args) {Outer out = new Outer (); out. print ();}}

 

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.