Dark Horse Programmer--java Foundation--Inner class

Source: Internet
Author: User
Tags local time modifiers



Dark Horse Programmer--java Foundation--Inner class

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Inner class

If class a requires direct access to the Members in class B , then class B needs to establish an object of class a . In this case , the class A is defined directly in class B for ease of design and access. You can do it. Class A is called an inner class . An inner class can directly access members in an external class. For external classes that want to access internal classes, you must create an object of the inner class.

Access rules for internal classes
1, the inner class can access the members of the external class directly, including the private.
Members in an external class can be accessed directly because the inner class holds a reference to an external class, in the form of an external class name. This
2, the outer class must establish an inner class object to access the inner class.

Access format
1, when the inner class is defined on the member position of the outer class, and is not private, it can be in an external other class. You can create an inner class object directly.
Format
The outer class name. Internal class Name Variable name = External Class object. Inner class object;
Outer.Inner in = new Outer (). New Inner ();

<span style= "FONT-SIZE:14PX;" >public class Innerclassdemo {public static void main (string[] args) {//outer out = new Outer ();//out.method ();//direct access to internal The members in the class. Outer.Inner in = new Outer (). New Inner (); In.function ();}} class Outer{private int x = 3;class inner//inner class {int x = 4;void function () {int x = 6; System.out.println ("Innner:" + x); System.out.println ("Innner:" + this.x); System.out.println ("Innner:" + outer.this.x);}} void method () {Inner in = new Inner (); In.function ();}} </span>

2, You can use some member modifiers to decorate privateandstaticwhen the inner class definition is on a member position in an external class.

1: Default modifier.

Direct access to inner class format: external class name . Internal class name Variable name = External Class object . Inner class object ;

Outer.Inner in = new outer.new Inner ();//This form is seldom used.

But this is a rare application because internal classes are defined internally for encapsulation. to get an inner class object is usually obtained by means of an external class. This allows the inner class object to be controlled.

2: private modifier.

usually the inner class is encapsulated and will be privatized because encapsulation does not allow direct access by other programs.

3: static modifier.

If the inner class is statically decorated, which is equivalent to an external class, access limitations occur and only static members in the outer class can be accessed.

Note: If a static member is defined in an inner class, the inner class must be static.

<span style= "FONT-SIZE:14PX;" >class outer2{private static  int x = 3;static class inner//static inner class {static void function () {SYSTEM.OUT.PRINTLN (" Innner: "+ x);}} Static Class Inner2{void Show () {System.out.println ("Inner2 Show");}} public static void Method () {//inner.function (); new Inner2 (). Show ();}} public class InnerClassDemo2 {public static void main (string[] args) {//TODO auto-generated method Stub//outer2.method (); Outer2.Inner.function ();//new outer2.inner (). function ();//Direct access to members within the inner class. Outer2.inner in = new Outer2.inner (); In.function ();}} </span>

The internal class is compiled with the file name: "External class name $ Internal class name . Java"

Why does an inner class have direct access to members in an external class?

That's because the inside holds a reference to an external class. This is a reference to the external class name .

An inner class can be defined on a member location in an external class, or in a local location in an external class

Inner class defined at local time

1, can not be modified by member modifiers

2, you can access members in an external class directly, because you also hold references in external classes. However, you cannot access the variables in the local area where it resides. Only the final decorated local variables can be accessed.

<span style= "FONT-SIZE:14PX;" >public class InnerClassDemo3 {public static void main (string[] args) {//TODO auto-generated method StubOuter1 out = n EW Outer1 (); Out.method (7);}} Class Outer1 {int x = 3;void method (final int a) {final int y = 4;class Inner {void function () {System.out.println (y);}} New Inner (). function ();}} </span>
anonymous inner class
1, the anonymous inner class is actually the shorthand format for the inner class.
2, the premise of defining an anonymous inner class:
The inner class must inherit a class or implement an interface.
3, Anonymous inner class format: New parent class or interface () {Defines the contents of a subclass}
4, in fact, the anonymous inner class is an anonymous subclass object. And the object is a little fat. Can be understood as an object with content.
5, it is best not to have more than 3 methods defined in an anonymous inner class.
<span style= "FONT-SIZE:14PX;" >public class InnerClassDemo4 {public static void main (string[] args) {//TODO auto-generated method Stubnew Outer4 (). f Unction ();}} Abstract class Absdemo {abstract void Show ();} Class Outer4 {int x = 3;public void function () {Absdemo d = new Absdemo () {int num = 9;void Show () {System.out.println ("Nu m=== "+ num);} VOID abc () {System.out.println ("haha");}}; D.show ();//D.ABC ();//compile failed;}} </span>




Dark Horse Programmer--java Foundation--Inner class

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.