Home > database >  The Java interface technology
The Java interface technology

Time:01-16

Java interface is a series of method statement, is a collection of method features, (interface is defined by the constants and abstract methods), due to the realization of the interface in no way, so it can be implemented in different classes, in order to realize the interface method, need after the interface inheritance to rewrite all the methods of the parent, so implementing an interface is implemented this class inherits the interface, the interface to realize the method interface defined syntax for: Publicinterface interface name {//define constants constant publicstaticfinal data type name=value;//define abstract method Publicabstract return value type method name (data type parameter names... )} in the interface definition points to note is: 1, in the interface is not allowed to instantiate an object, and only one access modifier public, 2, and provide the static in the interface default, final, the abstract keyword, in my opinion, because the Java in a subclass can inherit a parent class, and the interface is not affected by this, that is, a class can implement multiple interfaces, therefore the interface inheritance can make up for the lack of a, in addition, when defining interfaces can also implement multiple other interface, the implementation can be thought of as similar to class inheritance, interface implementation is the keyword implements, specific syntax is: publicclass the name of the class extends the name of the class implements the interface name,... {//this class must implement all of the method in the interface of} in swing interface programming, there are four very important interface may be used multiple times and they are addActionListener (ActionListenerl); AddMouseListener (MouseListenerl); AddMouseMotionListener (MouseMotionListenerl); AddKeyListener (KeyListenerl); They are all event listeners method, is used to implement the event source object listening, all of the graphical interface components (container components, element component) can be a source object events, where operator action on a component, then the component is the event source object, look at the four monitoring methods, including: addActionListener (ActionListenerl); Is used to capture the mouse to click on the event source (similar to button component) or keyboard enter action (box), to the parameter ActionListener object for processing addMouseListener (MouseListenerl); Is used to capture the mouse on the event source to enter, leave, press, release, click the action, and then to the parameters for processing addMouseMotionListener MouseListener object (MouseMotionListenerl); Is used to capture the mouse or drag action on the event source, and then to the parameters of the MouseMotionListener object for processing addKeyListener (KeyListenerl); Is used to capture the event source on the keyboard key press, release and tapping movements, then to KeyListener object for processing parameters in actual programming, we need according to the actual situation to choose to use different ways to listen, but it is important to note that a class can implement multiple interfaces, each implement an interface, we need all the methods are rewritten in the interface, add the listener to an event source the steps of the specific as follows: 1, create a new class inherits the corresponding monitoring method, according to the class names to instantiate an object, event source object passed as a parameter to the class, and 2, add an action objects to the event source monitoring method 3, the method of implementing an interface in the class, the following through a simple example to illustrate the implementation and monitoring method of use: implement a login interface, when click the login button to close the landing interface, a new form (temporary not consider verify password)
  • Related