Java Beginner: Interface technology Articles

Source: Internet
Author: User

Java Interface Technical Articles

Java interface is a series of methods of Declaration, is a collection of method features, (the interface is defined by constants and abstract methods), because there is no implementation of methods in the interface, so its methods can be implemented in different classes, to implement the method in the interface, you need to inherit the interface after the inheritance of the parent class of all methods to rewrite, So the implementation of the interface is also the implementation of the class inherits the interface, the implementation of the interface method

The syntax for an interface definition is:

Public interface Interface Name {

Defining constants

public static final data type constant name = value;

Defining abstract methods

Public abstract return value type method name (data type parameter name ...)

}

The points to note in the interface definition are:

1, an object is not allowed to be instantiated in the interface, and there is only one access modifier public.

2, the Static,final,abstract keyword is provided by default in the interface.

In my opinion, because a subclass in Java can inherit only one parent class, and the interface is not affected by the fact that a class can implement multiple interfaces, the inheritance of interfaces can compensate for this deficiency. In addition, it is possible to implement several other interfaces when defining an interface. This implementation can be thought of as similar to inheritance in a class.

The key word for the interface implementation is implements,

The specific syntax is: public class class Name extends class name implements interface name,... {

The class must implement all the methods in the interface

}

In the swing interface programming, there are four very important interfaces that may be used multiple times, they are addActionListener (ActionListener l) respectively;

Addmouselistener (MouseListener L);

Addmousemotionlistener (Mousemotionlistenerl);

Addkeylistener (KeyListener l); they are all event monitoring methods used to monitor the event source object, all graphical interface components (container components, element components) can be the event source object, the action of the operator on which component, Then the component is the event source object. Then look at these four ways of listening, which:

addActionListener (ActionListener L); is used to capture the mouse click on the event source (similar to a button component) or the keyboard carriage return action (input box) to the parameter ActionListener object for processing

Addmouselistener (MouseListener L); is used to capture the action of the mouse entering, leaving, pressing, releasing, and clicking on the event source, and then handing over to the parameter MouseListener object for processing

Addmousemotionlistener (Mousemotionlistenerl); is used to capture the mouse movement or drag action on the event source and then hand over to the parameter Mousemotionlistener object for processing

Addkeylistener (KeyListener L); is used to capture the keyboard keys on the event source press, release and strike action, and then hand over to the parameter KeyListener object for processing

In actual programming, we need to choose different listening methods according to the actual situation, but it is important to note that a class can implement multiple interfaces, each implementing an interface, we need to rewrite all the methods in the interface.

The steps to add a listener to an event source are as follows:

1. Create a new class that inherits the corresponding listener method, instantiate an object according to the class name, and pass the event source object as an argument to the class.

2. Add an action monitoring method to the event source object

3, the method of implementing the interface in this class.

Here is a simple example to illustrate the implementation of the interface and the use of the Listener method:

Implement a login interface, when the login button is clicked to close the login screen, a new form appears (do not consider verifying the login password)

Login class


Loginlistener class


Java Beginner: Interface technology Articles

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.