Analysis of the Role of Java internal classes in GUI Design (1)

Source: Internet
Author: User

We do not know much about Java internal classes. Here we will introduce in detail the role of Java internal classes in GUI Design in the form of actual code.

Java
Internal classes are rarely used in J2EE programming,
However, Window application programming is particularly common and mainly used for event processing. In fact, for non-Gui programming, internal classes are completely unnecessary.

The declaration and access control of internal classes are different from those of external classes. It is still quite difficult to use internal classes to write programs flexibly. Java invented this hard-to-understand thing, in other languages
But in Java, internal classes are also very important. Especially in GUI development, Event Response Processing relies entirely on internal classes.

Functions of internal classes can also be implemented using external classes, but sometimes internal classes are more clever.

Internal classes are divided into the following types based on their locations:

1. (common) internal class (the most common internal class, the definition of internal class is equal to that of Class Members ,)

2. Internal method class

3. Anonymous class

4. static internal class

5. Interface internal class

I. Internal class declaration and access

1. The internal class is declared directly inside the class. It can be declared as private, protected, public, or default access permission. This access permission convention is completely consistent with the external class.
Same.

2. The internal class automatically has access to all the Members (methods and attributes) of its peripheral class. If the name of an internal class is the same as that of an external class member
Use the following method to access: External class name. This. External member name, for example, outer. This. I ++; (see the example)

3. An external class object must be used to create an internal class object instead of a new one.

Format: External Object Name. New internal class Constructor

For example, to create an internal class iner object, you need to do this:

 
 
  1. Outer outer = new outer ();
  2. Outer. Inner iner = outer. New inner ();
  3.  
  4. /**
  5. * Internal class creation and initialization
  6. *
  7. * @ Author leizhimin 2009-7-17 13:51:52
  8. */
  9. Public class Outer {
  10. Private int I = 10;
  11. Private int y = 8;
  12.  
  13. Outer (){
  14. System. Out. println ("Call outer constructor: outer ");
  15. }
  16.  
  17. Public void saymsg (){
  18. System. Out. println ("outer class! ");
  19. }
  20.  
  21. Class inner {
  22. Int I = 1000;
  23.  
  24. Inner (){
  25. System. Out. println ("Call inner constructor: inner ");
  26. }
  27.  
  28. Void innermsg (){
  29. System. Out. println (">>>> inner class! ");
  30. Saymsg ();
  31. // Access the Member I of the internal class, which can also be written as this. I ++
  32. This. I ++;
  33. // Access the members I and Y of the external class
  34. Outer. This. I ++;
  35. Y --;
  36. }
  37.  
  38. Int Geti (){
  39. Return I;
  40. }
  41. }
  42.  
  43. Public void test (){
  44. Inner in = new inner ();
  45. In. innermsg ();
  46. }
  47.  
  48. Public int Geti (){
  49. Return I;
  50. }
  51.  
  52. Public void SETI (int I ){
  53. This. I = I;
  54. }
  55. }
  56.  
  57. Class test1 {
  58. Public static void main (string [] ARGs ){
  59. Outer outer = new outer ();
  60. Outer. Test ();
  61. System. Out. println (outer. Geti ());
  62. System. Out. println ("------- 1 --------");
  63.  
  64. Outer. Inner iner = outer. New inner ();
  65. Iner. innermsg ();
  66. System. Out. println (INER. Geti ());
  67. System. Out. println ("------- 2 --------");
  68.  
  69. System. Out. println (outer. Geti ());
  70. }
  71. }

Running result:

Call outer constructor: outer

Call the inner constructor: inner

 
 
  1. >>>>>Inner class!   
  2. Outer class!   
  3. 11   
  4. -------1--------  

Call the inner constructor: inner

 
 
  1. >>>>>Inner class!   
  2. Outer class!   
  3. 1001   
  4. -------2--------   
  5. 12   
  6.  
  7. Process finished with exit code 0  

Ii. Internal classes and interfaces

1. Internal classes can implement interfaces.

2. Internal classes are visible to each other, but not between internal classes.

 
 
  1. Public interface Foo {
  2. Void say ();
  3. }
  4.  
  5. Public interface bar {
  6. Void readme ();
  7. }
  8.  
  9. /**
  10. * Internal class implementation Interface
  11. *
  12. * @ Author leizhimin 2009-7-17 14:57:50
  13. */
  14. Public class Test2 {
  15. Public static void main (string [] ARGs ){
  16. Outer outer = new outer ();
  17. Foo F = outer. genfoo ();
  18. Bar B = outer. genbar ();
  19. F. Say ();
  20. B. readme ();
  21. }
  22. }
  23.  
  24. Class outer {
  25. Private class fooimpl implements Foo {
  26. Public void say (){
  27. System. Out. println ("say foo! ");
  28. }
  29. }
  30.  
  31. Private class barimpl implements bar {
  32. Public void readme (){
  33. System. Out. println ("say bar! ");
  34. }
  35. }
  36.  
  37. Public Foo genfoo (){
  38. Return new fooimpl ();
  39. }
  40.  
  41. Public Bar genbar (){
  42. Return new barimpl ();
  43. }
  44. }

Input result:

Say foo!

Say bar!

Process finished with exit code 0

Iii. Access Permissions

There are two types of external classes:

An external class embedded with an internal class declaration code is called a direct external class. The other is an external class that has no relationship with the internal class. It is called an external class.

In the same direct external class, all the methods between internal classes are visible to each other, including the main () of the direct external class.

To view the internal class members of a class in an external class, at least the class and member permissions of this internal class must be greater than or equal to protected.

 
 
  1. /**
  2. * Internal class implementation Interface
  3. *
  4. * @ Author leizhimin 2009-7-17 14:57:50
  5. */
  6. Public class Test2 {
  7. Public static void main (string [] ARGs ){
  8. Outer o = new outer ();
  9. Outer. Bar B = O. genbar ();
  10. B. readme ();
  11. }
  12. }
  13.  
  14. Class outer {
  15.  
  16. Protected class Foo {
  17. Protected void say (){
  18. System. Out. println ("say foo! ");
  19. }
  20.  
  21. Private void test (){
  22. System. Out. println ("---- test ------");
  23. }
  24. }
  25.  
  26. Protected class bar {
  27. Protected void readme (){
  28. System. Out. println ("say bar! ");
  29. New Foo (). Test ();
  30. }
  31. }
  32.  
  33. Public Foo genfoo (){
  34. Return new Foo ();
  35. }
  36.  
  37. Public Bar genbar (){
  38. Return new bar ();
  39. }
  40. }


This question is transferred from http://developer.51cto.com/art/201002/183375.htm

I think it's good. Add it to your favorites and hope it can help you.

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.