Java internal class

xiaoxiao2021-03-06  113

//Staticcls.java

Public class staticcls {public static void main (String [] args) {// Outercls Ci = new staticcls (). New OuterCls (); // method 3 replace 2.1 // Outercls.innercls oi = new outercls.innercls (); //Method 1 OuterCls Ci = new outercls (); // method 2.1 Outercls.innercls oi = ci.new innercls (); // method 2.2}} // if We use method 3, We Must Put Outercls Into The StaticCls

class OuterCls {OuterCls () {System.out.println ( "OuterCls");?} // public static class InnerCls {// 1 public or default Please have a try public class InnerCls {InnerCls () {System.out.println ("Innercls");}}}

//xtest.java

Class Outer {static int out {= 0; int out __non_stat = 1; static class stataintinner {static int stat = 2;

// Static internal class static member can access the static variables of the external class Static int stat_test = Outer_Stat; public void test () {

// Static internal class non-static member can access the external class static variable system.out.println ("Outer_stat =" Outer_STAT);}} Class Nonstaticinner {public void test () {

// Non-static member of non-static internal classes can access the exterior class static variable system.out.println ("Outer_Stat =" Outer_STAT);

// Non-static members of non-static internal classes can access the unstatic variables of the external class System.out.Println ("Outer_non_stat = Outer_non_stat);

// Non-static member of non-static internal classes can access static variables of static internal classes under the same external class System.out.println ("staticinner.stat =" staticinner.stat);}}} // static internal class There is a static member, not static members, no static members

Public class xtest {public static void main (string [] args) {new outer (). New nonstaticinner (). Tester (); system.out.println ("----------") New outer.staticinner (). Tester ();}}

Java Technical Special Summary Source: IT Network Academy May 8, 2003 11: 6

Some types of definitions include the definition of another class. This is called an internal class (INNER Class). For example: public class outernal class {public class innerclass {// --- this is a inner class.}} InnerClass class nest in OuterClass, and is declared as public, so it is accessed by objects other than OuterClass classes. The InnerClass class is not declared as a static member of the OuterClass class, so any InnerClass object cannot be generated unless an OuterClass object is generated. However, when you declare a class object that contains nested classes, it does not necessarily generate an object of nested classes, which is primarily determined by the constructor of the class containing the nested class. Example 1: class OuterClass {class InnerClass {}} public class Test {public static void main (String [] args) {OuterClass out = new OuterClass (); OuterClass.InnerClass in = out.new InnerClass ();}} We used OuterClass Out = New OuterClass (); statement generates an OuterClass class object. Then use OuterClass.innerClass IN = out.new innerclass (); statement generates an internal class object with an example of an external class. The two statements in the main () method can also be replaced with the following statement: OuterClass.innerClass in = new outrclass (). New innerclass (); short: In a class (TEST), create another class (OuterClass The non-static internal class (InnerClass) must have an instance of this external class (OuterClass). And the declaration of this internal object must also be in the form of OuterClass.innerClass. Example 2: Public class test {class innerclass {} 1 public static void main (string [] args) {innerclass in = new test (). New innerclass ();}} In the main () method, instantiate the interior of this class Classs can be in the form of the above example. Example 3: Public class test {class innerClass {} innerclass in = new innerclass (); public static void main (String [] args) {}} In addition to the main () method, instantiate the internal classes of this class and us usually The operation is the same. Example 4: Public Class Test {Class InnerClass {} public void method () {innerclass in = new innerclass ();} public static void main (String [] args) {}} Instantiate The internal classes of this class are also the same as our usual operation. In order to make the internal class objects do not depend on external classes, you can declare the internal class as static.

Example 5: Class OuterClass {}} public class test {public static void main (string [] args) {2 OuterClass.innerClass in = new outerclass.innerclass ();}} We can see that we can see: Objects Creating InnerClass classes no longer need OuterClass classes. But still use OuterClass.innerClass to declare in object. Example 6: class Outer {static int outer_stat = 0; int outer_non_stat = 1; static class StaticInner {static int stat = 2; static int stat_test = outer_stat; public void tester () {System.out.println ( "outer_stat =" outer_stat);}} class NonStaticInner {public void tester () {System.out.println ( "outer_stat =" outer_stat); System.out.println ( "outer_non_stat =" outer_non_stat); System.out.println ( "StaticInner .stat = " staticinner.stat);}}} public class test {public static void main (String [] args) {new outr (). New nonstaticinner (). Tester (); system.out.println (" - ----------- "); New Outer.Staticinner (). Tester ();}} Output: Outer_stat = 0 Outer_non_stat = 1 staticinner.stat = 2 3 -------- ---- Outer_stat = 0 NonStaticinner class System.out.println ("Outer_stat =" Outer_STAT); statement Description: Non-static members of the non-static internal class can access the external class static variables. System.out.println ("Outer_non_stat =" Outer_non_stat); statement Description: Non-static members of non-static internal classes can access unstatic variables of the external class. System.out.println ("staticinner.stat =" staticinner.stat); statement description: Non-static members of non-static internal classes can access static variables of static internal classes under the same external category. STATICINNER class System.out.println ("Outer_Stat =" Outer_STAT); statement description: Non-static members of static internal classes can access static variables of external classes. Static int stat_test = Outer_Stat; statement Description: Static members of the static internal class can access the static variables of the external class. Note: Static internal classes can have static members, rather than static internal classes.

Example 7: Class Outer {public void test () {class inner {}}} public class test {public static void main (string [] args) {}} can define a class in a method, this class is called local interior class. It can only be locally, that is, the object of the local internal class is generated in the method that appears in this class. This process is suitable for use in a method to use a special class, while this class is not required at other places. The local internal class can reference the variables declared in the way they define their definition, and when this variable is Final type. Please see Example 8. Example 8: Class Outer {Public Void Tester () {Final Int i = 5; Class Inner {INT J = I; 4} Inner IN = New Inner ();}} public class test {public static void main (String " Args) {}} Last to explain anonymous internal classes. Simply put: Anonymous internal classes are the internal classes without names. Under what circumstances, do you need an anonymous internal class? If you meet some of the following conditions, use an anonymous internal classes to be appropriate: • Use only an instance of the class. • The class is used immediately after definition. · Class is very small (Sun recommended below 4 lines of code) · Naming to class does not cause your code more easily understood. When using anonymous internal class, remember the following four principles: • Anonymous internal classes cannot have a constructor. · Anonymous internal classes cannot define any static members, methods, and classes. · Anonymous internal classes cannot be public, protected, private, static. · Click an instance of an anonymous internal class. Below is an example of an anonymous internal class. Readers Please pass the example to experience the usage of anonymous internal classes.

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

New Post(0)