In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the role of Java internal classes in GUI design". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of Java internal classes in GUI design".
Java inner classes are rarely used in J2EE programming, but are particularly common in window application programming, mainly for event handling. In fact, if you do non-GUI programming, you don't need inner classes at all.
Internal class declaration, access control is equal to the external class is different, to flexibly use the internal class to write the program, or quite difficult, Java invented this difficult thing, in other languages is not available, but in Java, the internal class is also very important, especially when doing GUI development, the event response processing all depends on the internal class.
The functions done by inner classes can also be implemented using external classes, but sometimes they are more ingenious.
Inner classes can be divided into the following categories according to their location:
1. (ordinary) inner class (the most common inner class, the definition of inner class and the level of class members,)
2. Method inner class
3. Anonymous class
4. Static inner class
5. Interface inner class
I. Internal class declaration and access
1. The inner class is declared directly inside the class. It can be declared as private, protected, public, or default access, which is exactly the same as the external class.
2. The inner class automatically has access to all members (methods, properties) of its peripheral class. If the names of the inner class and the outer class members are exactly the same, and you want to access the outer class members in the inner class method, you need to access it in the following way: external class name .this. External member names, such as Outer.this.i++; (see example)
3. You must use an external class object to create an internal class object instead of directly new one.
The format is: external object name. new inner class constructor
For example, to create an inner class iner object, you need to do this:
Outer outer = new Outer (); Outer.Inner iner = outer.new Inner (); / * * Internal class creation and initialization * * @ author leizhimin 2009-7-17 13:51:52 * / public class Outer {private int i = 10; private int y = 8; Outer () {System.out.println ("calling Outer constructor: outer") } public void sayMsg () {System.out.println ("Outer class!");} class Inner {int I = 1000; Inner () {System.out.println ("call Inner constructor: inner") } void innerMsg () {System.out.println ("> Inner class!"); sayMsg (); / / visit the inner class's own member I, or write it as this.i++ this.i++ / / access members of the external class I and y Outer.this.i++; Ymuri;} int getI () {return I }} public void test () {Inner in = new Inner (); in.innerMsg ();} public int getI () {return I;} public void setI (int I) {this.i = I }} class Test1 {public static void main (String [] args) {Outer outer = new Outer (); outer.test (); System.out.println (outer.getI ()); System.out.println ("- 1 -") Outer.Inner iner = outer.new Inner (); iner.innerMsg (); System.out.println (iner.getI ()); System.out.println ("- 2 -"); System.out.println (outer.getI ());}}
Running result:
Call the Outer constructor: outer
Call the Inner constructor: inner
> Inner class! Outer class! 11-1-
Call the Inner constructor: inner
> Inner class! Outer class! 1001-2-12 Process finished with exit code 0
II. Internal classes and interfaces
1. Inner classes can implement interfaces.
2. Inner classes are visible to each other, but not all methods between inner classes are visible.
Public interface Foo {void say ();} public interface Bar {void readme ();} / * * Internal class implementation interface * * @ author leizhimin 2009-7-17 14:57:50 * / public class Test2 {public static void main (String [] args) {Outer outer = new Outer () Foo f = outer.genFoo (); Bar b = outer.genBar (); f.say (); b.readme () }} class Outer {private class FooImpl implements Foo {public void say () {System.out.println ("say foo!") }} private class BarImpl implements Bar {public void readme () {System.out.println ("say bar!");}} public Foo genFoo () {return new FooImpl () } public Bar genBar () {return new BarImpl ();}}
Enter the result:
Say foo!
Say bar!
Process finished with exit code 0
III. Access rights
There are two external classes:
An external class with embedded internal class declaration code is called a direct external class. The other is an external class that has nothing to do with the inner class, which is called the outer class.
In the same direct external class, all methods between the inner classes are visible to each other, contained in the main () of the direct external class.
In an external class, to see the inner class members of a class, at least the class and member permissions of the inner class are greater than or equal to protected.
/ * * Internal class implementation interface * * @ author leizhimin 2009-7-17 14:57:50 * / public class Test2 {public static void main (String [] args) {Outer o = new Outer (); Outer.Bar b = o.genBar (); b.readme () }} class Outer {protected class Foo {protected void say () {System.out.println ("say foo!");} private void test () {System.out.println ("- test-") }} protected class Bar {protected void readme () {System.out.println ("say bar!"); new Foo () .test () }} public Foo genFoo () {return new Foo ();} public Bar genBar () {return new Bar ();}}
Fourth, the method inner class
The method inner class is visible only within the method, and the method inner class can be defined anywhere in the method.
/ * * @ author leizhimin 2009-7-17 14:57:50 * / public class Test2 {public static void main (String [] args) {Outer outer = new Outer (); Foo f = outer.genFoo (); Bar b = outer.genBar (); f.say () B.readme ();}} inner class class FooImpl implements Foo {public void say () {System.out.println ("say foo!") in class Outer {public Foo genFoo () {/ / method }} return new FooImpl ();} public Bar genBar () {Bar b = null If (true) {/ / inner class class BarImpl implements Bar {public void readme () {System.out.println ("say bar!") anywhere }} b = new BarImpl ();} return b;}}
Running result:
Say foo!
Say bar!
Process finished with exit code 0
5. Anonymous category
An anonymous class does not give a class name, but directly defines a class that usually implements some kind of interface or abstraction. Access to anonymous classes is even less worth discussing, just take a look at an example.
In some multithreaded programs are more common, a little abnormal, hehe.
/ * Anonymous class. * * @ author leizhimin 2009-7-17 15:56:17 * / public class Test3 {public Foo f = new Foo () {public void say () {System.out.println ("O (∩ _ ∩) O ha ~!");}} Public Foo test () {return new Foo () {public void say () {System.out.println ("say foo!");}} } public static void main (String [] args) {Test3 t = new Test3 (); t.f.say (); t.test (). Say ();} interface Foo {void say ();}
Running result:
Say foo!
Anonymous initialization of Process finished with exit code 0 / * ordinary classes * * @ author leizhimin 2009-7-17 16:13:31 * / public class Fk {private String x; public Fk (String x) {this.x = x } @ Override public String toString () {return "Fk {" + "x"+ x +'\'+'}'' }} class Test4 {public Fk hehe () {/ / remove the following pair of curly braces, hehe return new Fk ("fk") {};} public static void main (String [] args) {Test4 t = new Test4 () Fk f = t.hehe (); System.out.println (f);}}
Running result:
Fk {Xerox FK'}
Process finished with exit code 0
There is also a classic example that has to be mentioned, from thining in java, with changes:
Interface Service {void method1 (); void method2 ();} interface ServiceFactory {Service getService ();} class Implementation1 implements Service {private Implementation1 () {} public void method1 () {System.out.println ("Implementation1 method1");} public void method2 () {System.out.println ("Implementation1 method2") } public static ServiceFactory factory = new ServiceFactory () {public Service getService () {return new Implementation1 ();}};} class Implementation2 implements Service {private Implementation2 () {} public void method1 () {System.out.println ("Implementation2 method1");} public void method2 () {System.out.println ("Implementation2 method2") } public static ServiceFactory factory = new ServiceFactory () {public Service getService () {return new Implementation2 ();}};} public class Factories {public static void serviceConsumer (ServiceFactory fact) {Service s = fact.getService (); s.method1 (); s.method2 () } public static void main (String [] args) {serviceConsumer (Implementation1.factory); serviceConsumer (Implementation2.factory);}}
This app gives us a lot of thinking, so I won't say it. Different people will feel differently when they read it.
The ingenious use of inner classes will make your code very powerful, if you want to describe it, it is: when you don't understand it, you feel like a ghost, and when you read it, you feel miraculous. However, there are so many codes that it is difficult for others to understand, and it is even more difficult to understand your train of thought. He he!
VI. Static inner class
The static inner class is the inner class of static class, which is characterized by that it cannot access the non-static members of the external class. When you want to create a static inner class object, you don't need an external class object, you can directly:
New external class name. Inner class construction method
To create, give an example:
/ * * static inner class * * @ author leizhimin 2009-7-17 16:53:05 * / public class Outer {public static int i = 500; protected static class Inner {int I = 100; String name; Inner (String name) {this.name = name } void sayHello () {System.out.println ("Hello" + name); Outer.i++;}} public Inner genInner (String name) {return new Inner (name) }} class Test {public static void main (String [] args) {Outer.Inner in1 = new Outer.Inner ("1111"); in1.sayHello (); System.out.println (Outer.i); Outer.Inner in2 = new Outer () .genInner ("2222") In2.sayHello (); System.out.println (Outer.i);}}
Running result:
Hello 1111
five hundred and one
Hello 2222
five hundred and two
Process finished with exit code 0
Internal classes of the interface
The inner classes of the interface are automatically public static, which is equivalent to defining a variable type for the interface, which is used in the design of java, such as in HashMap:
Static class Entry implements Map.Entry
Let me give you an example.
/ * author leizhimin 2009-7-17 17:20:28 * / public interface AInterface {void readme (); class Inner1 implements AInterface {public void readme () {System.out.println ("I am an interface inner class") } class Main {public static void main (String [] args) {AInterface.Inner1 in1 = new AInterface.Inner1 (); in1.readme ();}}
8. Nesting of internal classes
The so-called inner class nesting means that the inner class is defined in the inner class. In fact, this kind of usage has never been seen before, so try to write a simple example:
/ * nested inner classes * * @ author leizhimin 2009-7-17 17:33:48 * / public class Outer {private void f0 () {System.out.println ("f0");} class A {private void a () {f0 () System.out.println ("a");} class B {protected void b () {a (); System.out.println ("b") }} class Test {public static void main (String [] args) {Outer o = new Outer (); Outer.An a = o.new A (); Outer.A.B b = a.new B () B. b ();}}
Running result:
F0
A
B
Process finished with exit code 0
VIII. Inheritance of inner classes
The inheritance of inner classes can inherit either inner classes or external classes.
/ * inheritance of inner classes, can inherit inner classes or external classes * * @ author leizhimin 2009-7-22 13:50:01 * / public class Outer {class Inner {void doSomething () {System.out.println ("Inner doing...") }} class Inner2 extends Inner {void doSomething () {System.out.println ("Inner2 doing...");} void readme () {System.out.println ("HeHe!") } class Test {public static void main (String [] args) {Outer outer = new Outer (); Outer.Inner in = outer.new Inner (); Outer.Inner2 in2 = outer.new Inner2 (); in.doSomething (); in2.doSomething () In2.readme ();}}
Running result:
Inner doing...
Inner2 doing...
HeHe!
Process finished with exit code 0
Thank you for your reading, the above is the content of "what is the role of Java internal classes in GUI design". After the study of this article, I believe you have a deeper understanding of what the role of Java internal classes in GUI design is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.