Java Interface Details

Source: Internet
Author: User

For beginners, the interface is not very easy to understand. I will post an article from a master to share with you!
Let's look at a class
Class {
Private int;
Public int geta (){
Return;
}
}
The attributes of this class are private and cannot be accessed from the outside, but the outside can be accessed through public methods. Let's talk about the public side of a class.
Method is the external interface of this class. Usually
The attributes of a class are private and the methods are mostly public. The external world can only use these public methods to define the class. This is also Java Encapsulation
. If a class has no public attributes,
There is no public method, and this class is unusable. Therefore, we need to provide external interfaces for a class.
A Method in a class not only describes what it wants to do, but also describes how it is done ". Make a mistake
Example: A method of killing people. From the name, you know what to do, but you cannot see how to do it ". The content of the Method
Understand "How to do it ".
Class killer {
Private string name;
Private int age;
Private string phone;
Private string ADDR;
......
Public void kill (person p ){
Qiang = new Qiang ("AK47 ");
Qiang. Fire (P );
}
}
The kill method of this class describes the process of killing. If you don't want to kill people in this way. Because you are BT, you want to use poison to kill people.
The content of this class needs to be modified. However, there are many other "customers" that need to be killed in different ways. What should we do? One
A good solution is to define "what to do" instead of "how to do ".
Interface killer {
Public void kill (person P );
}
The interface describes "what to do", and the class that implements this interface, that is, the implementation class needs to explain "How to do it ".
Class killer1 implements killer {
Public void kill (person p ){
Qiang = new Qiang ("AK47 ");
Qiang. Fire (P );
}
}
Class killer2 implements killer {
Public void kill (person p ){
Bane = new bane ();
P. Eat (Bane );
}
}
Public class test {
Public static void main (string [] ARGs ){
Killer jingke = new killer1 ();
Person yingzheng = new person ();
Jingke. Kill (yingzheng );
}
}
The interface can separate "what" from "how. This brings many benefits to Java. Although the amount of Code has increased
The maintainability of the program is enhanced. Our programs can be split. Just like a computer, it can be split into many components. I keep thinking,
If I can split my MP3 headphones, I don't have to buy another one when the headset only breaks down.
However, the above example does not show much benefits. You may say that if your purpose is not to modify the code
To use killer2 to complete the task, you still need to modify the main method to killer jingke = new killer2 ();. No error,
However, you can use a factory to complete the above tasks. In other words, instead of using the new statement to obtain the killer object
Factory to obtain the killer object.
Public class killerfactory {
Public static killer getkiller (){
Return new killer1 ();
}
Public static killer getkiller (string path) throws exception {
Properties prop = new properties ();
Prop. Load (New fileinputstream (PATH ));
String classname = prop. getproperty ("killer ");
Class clazz = Class. forname (classname );
Return (killer) clazz. newinstance ();
}
}
The Code has indeed increased a lot, but it has brought many benefits to system maintenance and system upgrade in the future.
The relationship between the faucet and the outlet pipe. We need to install the faucet on the outlet pipe. If one day we need to change the faucet
Remove the old faucet and install the new faucet on the outlet pipe. If the faucet and outlet pipe are integrated, they cannot be split.
What should I do? Or, we can only install the faucet instead of the shower, which makes our life very inconvenient. We can
It is understood as the connection method of the outlet pipe. The connection object is the "outlet device", and this "outlet device" is an interface. And tap and shower
The bath is an implementation class of this interface. But where is the interface? What does it look like? We didn't see it. It is a standard, connection location
Diameter and outer diameter. Screw weight density. That's just why you can connect to a USB device on your computer. If the computer and a USB
Other USB devices cannot be used when the backup is welded together. The computer uses electronic devices that implement USB interfaces, while our U
Disk, MP3, mobile hard disk, and mouse are all USB interfaces.
Programs Written in Java are the same as devices in our real life. For example, we want all parts of the computer to be replaced. For example
If the memory slot and memory stick on the motherboard are not attached. We said that the memory stick does not implement the XX interface. Java is fully object-oriented, but oriented
Objects are the most familiar ones. Object-oriented is not simple, but we are too familiar with it. So it is very convenient for us to learn Java. In
In the real world, all standards are interfaces in Java. The number of cigarettes in a box and the length of cigarettes are all standard. A large disk
Small. JDBC in Java is a standard, which is achieved by major database vendors and third-party vendors. JDBC only describes the purpose,
It does not explain how to accomplish the goal.
Object-oriented is just in front of us, but sometimes we don't pay much attention to it. I hope you will be more connected to the real society when learning Java in the future. This way
It is helpful for your understanding.
The amount of code is increased, but it is convenient for later maintenance and upgrade. Software companies sell class files to customers, not java files.
. If your customer needs to change the killer object, you only need to modify the resource file.
Next we will look at a Timer class. Now everything is automated, such as air conditioners, televisions, and washing machines. The timer is used.
Class. By the way, there is also a time bomb to use. Can we write only one Timer class and apply it to all the devices that require a timer?
What about? The answer is yes. We need this class.
Well, let's analyze what the timer needs to do. The timer should have the start and stop methods. After the timer is started
Execute an action after a certain period of time. The time interval is long, and the action to be executed may be to output a string or
Print a job. The specific task is done by the timer user. When defining a timer, you do not know what to do.
Public class Timmer {
Private long time;
Private action;
Public Timmer (){}
Public Timmer (long time, action Action ){
This. Time = time;
This. Action = action;
}
Public void start (){
State = start;
If (Th = NULL ){
Th = new thread (){
Public void run (){
While (State = Start ){
Try {
Thread. Sleep (time );
Action. Action ();
} Catch (exception e ){
}
}
}
};
}
Th. Start ();
}
Public void stop (){
State = stop;
}
Public void settime (long time ){
This. Time = time;
}
Public void setaction (action Action ){
This. Action = action;
}
Public long gettime (){
Return (this. Time );
}
Public action getaction (){
Return (this. action );
}
}
Action is an interface with only one method, that is, the task to be completed. We call this interface method when the timer starts. While
The object of this action interface represents an action, which is the action to be completed by the user.
Public interface action {
Public void action ();
}
Public static void main (string [] ARGs ){
Timer T = new timer (2000, new action (){
Public void action (){
System. Out. println ("Hello world! ");
}
});
T. Start ();
Javax. Swing. joptionpane. showmessagedialog (null, "Click OK to stop the timer ");
T. Stop ();
System. Exit (0 );
}
This is a typical callback method example. Java uses a large number of listeners in AWT. All these listeners are callback methods. In XML
The callback method is also used to parse XML documents in the parser.
The interface is more abstract than the abstract class. Abstract classes must be inherited by subclasses, while Java is a single inheritance. Therefore, abstract classes are restricted. Different interfaces,
A class can implement multiple interfaces. It is like the relationship between humans and programmers. It can be said that programmers are subclasses of humans. If programmers are
One interface. It would be better to use human subclasses to implement it. This subclass can also implement the accounting interface and musician interface.
Interfaces are widely used in struts2.0, spring, hibernate, and other frameworks. We are concerned about the relationship between an interface and another interface.
. It does not care about the relationship between an implementation class and another interface implementation class. In the objective world, interfaces are widely used during conversations,
But we didn't pay attention to it. For example, our company needs a programmer (an object that implements the programmer interface ). In class, the lecturer has
A computer that writes on the whiteboard with a whiteboard pen (the computer is an interface, and the whiteboard and whiteboard pen are also interfaces ). The lecturer hopes that students can learn from the club
Knowledge (both instructors and students use interfaces ).

Related Article

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.