In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to implement the interface in Java". In daily operation, I believe many people have doubts about how to realize the interface in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to realize the interface in Java". Next, please follow the editor to study!
The role of interface
The role of interfaces is simple: interfaces are used to mark classes, different classes belong to different interfaces (through upward transformation), management interfaces are much more convenient than managing a variety of classes, interfaces reflect an abstract point of view, what is abstraction? Abstraction is "taking away the part of the image".
Use interfaces to solve problems
Problem: now we want to write a class to connect to the database for users to use, there are two functions: one returns the Connection object, the other is to close the database, close (), the general solution is to write a class for each database, and then use a specific class according to the database the user uses.
All right, let's see what's wrong with this:
(1)。 First of all, each class should have duplicate code, causing the code to swell.
(2)。 Secondly, the most important thing is that we do not know what database users use, which may be Oracle, mysql, sqlserver, etc., which is difficult to solve.
Solution:
First, let's define the interface:
Public interface DataBase
{
Java.sql.Connection openDB (String url,String user,String password)
Void close ()
}
We define two methods, openDB returns a Connection object, and close () closes the database
The specific implementation is in the class that implements the DataBase interface
Let's look at the implementation:
Import java.sql.*
Public class Mysql implements DataBase
{
Private String url= "jdbc:mysql:localhost:3306/test"
Private String user= "root"
Private String password= ""
Private Connection conn
Public Connection openDB (url,user,password)
{
/ / the code to connect to the database
}
Public void close ()
{
/ / close the database
}
}
Class mysql implements the DataBase interface, and there are also classes such as oraclesql that implement the DataBase interface
These classes are all attributed to the DataBase interface, so how can they be used in applications?
We want to define the DataBase object myDB and manipulate the database through myDB without distinguishing which class.
Another problem: we are not allowed to instantiate interfaces in Java, such as DataBase myDB=new DataBase ()
We can only myDB=new Mysql () or myDB=new Oracle (). In this way, we must also specify which object to instantiate, as if the previous efforts have been in vain! So what do we do? we need a factory:
Public class DBFactory
{
Public static DataBase Connection getConn ()
{
Return (new Mysql ())
}
}
The instantiated code becomes: myDB=DBFactory.getConn ()
The interface is not responsible for any specific operation in the whole process, and if other programs want to connect to the database, they only need to construct a DB object to OK, regardless of how the factory class changes. This is the meaning of the interface-abstraction.
At this point, the study on "how to implement the interface in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.