Java internal class

xiaoxiao2021-03-06  50

Java internal class

This article will explain the internal class, hope to communicate with you.

Simple internal class definition is like this:

Class a {

Class B {}

} Such classes are called internal classes and are also known as internal hidden categories.

From simply in-depth analysis of the characteristics of internal classes.

Class OuterClass

{Static class a // static internal class

{Public a ()

{System.out.println ("Test $ A!");}}

Class B // Non-static internal class

{Public b ()

{System.out.println ("TEST $ B!");}}

Public void disp ()

{

Final Int a = 10; int b;

Local internal classes in the Class C // member function

{Public c ()

{System.out.Println ("in Class C A =" a);

//System.out.println (B);

}

}

C c = new c ();

}

Public Class Test Extends OuterClass

{

Public static void main (string args [])

{Outerclass.a a = new outerclass.a ();

// Establish a static internal class object

B b = new outerclass () .new b ();

/ / Establish an object of non-static internal classes

// Note this OuterClass (). New b (); equivalent to generating an external class, then generate internal class objects using an external class object

Outerclass t = new outerclass ();

t.disp ();

// Call an object method in the form of an external object, the newly established object C.

}

}

Note that the above B is run at runtime because it is class attribute.

Class OuterClass

{

Static class a {} // static internal class

Class B {} // Non-static internal class

Public void disp ()

{

Class C {} // Local internal class

}

}

Compiled results:

Outerclass.class

Outerclass $ a.class

Outclass $ B.CLASS

Outerclass $ 1 $ C.CLASS

Remember the following sentences:

1. An internal class object can access all attributes and methods for creating its external class objects (including private parts).

// You can close the glasses, and the internal class is equivalent to a method, of course, you can access this external class.

// All methods and properties, private methods and attributes belong to external classes, of course, is equivalent to internal classes.

2. For other classes in the same package, the internal class can hide. (Typically modified internal classes)

// Only in the internal class can define a Class of the private type, because this time the compiler has regarded this class as the member of this class, but in general use, it is the so-called "top class", can't Using Private, you can only be public or Friendly.

If you want to guarantee that a class does not generate any object, in the constructor, declare the constructor into private.

3. Internal class can be defined in the method, called a local internal class, but it can only use the Final constant in the method.

/ / Define the class within a method, and is also a local internal class. This class can only use the final constant in the method. Note that this constant is in a method, then can you use a constant in a class? ?

Of course, it is ok because constants in the class are visible in one method.

//

If a class is written in an IF, such as this:

Class a {

INT A = 10;

IF (a! = 10) {

Class b {

B () {

System.out.println (a);

}

}

}

}

How many errors will be compiled? First, the A is not defined as final, you have a circle.

Class B is also generated, but the IF field is exposed.

4, internal classes can be defined as abstract classes

// Abstract class can also be in the internal class

5. Non-static internal classes cannot declare this class Static member

// Only a static internal class can declare a Static member,

Class a {

Static class b {

/ / If it is not a Static class here, it is not possible to declare this GG method.

Static void gg () {

INT A = 100;

System.out.println (a);

}

}

}

Class aa {

Public static void main (string args []) {

A.B hh = new a.b ();

HH.GG ();

}

}

Use internal classes to write event drivers very convenient

This will have a good explanation when you write events.

Anonymous internal class

In some cases, we only need an object of the internal class, then we don't have to name the internal class, there is no name for internal classes, we call anonymous internal class

Public Class a Extends Applet

{Public void init ()

{AddMouseListener (new b ());

}

Class B Extends MouseAdapter

{Public void mousepressed (MouseEvent Me)

{ShowStatus ("Mouse Pressed.");

}

}

Use anonymously invisible to modify:

Import java.applet. *;

Import java.awt.event. *;

Import java.awt. *;

Public class anonymousinnerclassdemo extends applet

{Public void init ()

{AddMouseListener (new mouseadapter ()

{Public void mousepressed (MouseEvent Me)

{ShowStatus ("Mouse Pressed");}});

}

}

Below is an example of this Think in Java

Public class pr {

Public Concents Cont () {

Return new conces () {

Private INT i = 11;

Public int value () {Return I;}

}; // This is a comma

}

}

This contents is generated by the default constructor, an anonymous class is also a new class to transition to the parent class.

So what should I do if a parent class does not have a default constructor?

Then only call a super ().

Class Noname {

WARPPING WRAP (INT X) {

Return new warpping (x) {

Public int value () {

Return super.value () * 47;

}

}

}

Public static void main (string s []) {

Noname P = New Noname ();

WARPPING W = P.Wrap (10);

}

}

If an external object is used in an anonymous internal class

It must be guaranteed to be Final

Public class pp {

Public DD DEST (FINAL STRING DEST, FINAL FLOAT Price) {

Return new dd () {

PRIVATE INT COST;

{

Cost = math.round;

IF (Cost> 100)

System.out.println ("over budget");}

}

}

Publc static void main (string [] arg) {

PP H = New PP ();

DD D = H.DEST ("Haha", 11.111);

}

}

Ok, let's write this, my email is

Flyboy1234@163.com

I hope that all Java enthusiasts will contact me, how many exchanges!

-------------------------------------------------- -flyboy1234

转载请注明原文地址:https://www.9cbs.com/read-80504.html

New Post(0)