Java's inner and outer classes

Source: Internet
Author: User
Java's inner and outer classes-general Linux technology-Linux programming and kernel information. For more information, see the following. What is inner class?
The inner class is a class defined inside another class. The class that defines this class is called the outer class. The inner class has the largest
Benefits
It has access to all variables and methods defined in the outer class, which is also suitable for creating the inner class.
Adaptation
Another advantage of the inner class is that you can see that they instantiate an interface instance, the following code (
Yespress
Click the Event code ):
// Outer class is "Frame1"
Button1.addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Button?action=med (e); // The button=action=med (e) method is defined in Fr
In ame1
}
});
Where:
Java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Button?action=med (e );
}
}
This is an internal class defined! In fact, ActionListener is an interface, and the content in the subsequent {} is
Inner Layer
The only method of the class: public void actionreceivmed (ActionEvent e); this inner class does not
Any
Attribute. The only action in this method is to call the button-clicked method ---- button?action=med
(E)
---- This method is defined in Frame1 of the outer class: our processing is in this method. For example, we can write:
Void button?action=med (ActionEvent e ){
System. exit (0 );
}
Here, we can see that the inner class calls the method of the outer class ---- button?action=med (e ).
, We
I noticed "new" in front of the definition of this inner class. She instantiated this inner class!
Anonymous class and naming class
In the above Code, the defined inner class has no class name, because this class is only used once, is
Secondary"
Class, that is, the anonymous class. Do not worry about the class name. Java will help us with this. After compilation, you can
Contents
These classes are found in the directory. They have the following names: Frame1 $1. class, Frame1 $2. class, and so on.
.
We can also use this technology, for example:
Button newButton = new Button (){
Public void someNewMethod (){
/* Defination of extra method here */
}
};
Here, the newButton object is a normal java. awt. Button, which contains an additional method defined for it.
When you want to create multiple objects in an inner class, you must use the naming class instead of the anonymous class.
Nature
The Code is as follows:
Public Frame1 extends DecoratedFrame {
...
Public Frame1 (){
...
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}
} // End of Frame1 constructure
Private void jbInit () throws Exception {
...
Button1.addActionListener (new Button1Adapter ());
...
} // End of jbInit () method
Class Button1Adapter implements java. awt. event. ActionListener {
Public void actionreceivmed (ActionEvent e ){
Button1.actionreceivmed (e );
}
}
Void button?action=med (ActionEvent e ){
/* Your own statements */
}
}
A normal definition is provided for a class named Button1Adapter. This class implements ActionListener
Port.
New creates an instance of the adapter. You can register other button1 objects together. The inner class will have such
File
Name: Frame1 $ Button1Adapter. class.
The above code can also be implemented without an internal class. However, the standard method should be the internal class. If you are interested
Then,
You can also try to implement the above Code without using an internal 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.