In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to define interface and inheritance in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to define interface and inheritance in Java".
Suppose I have many classes, Mysql.java, SQLServer.java, Oracle.java, and DB2.java, that connect to different databases, return a single Connection object, and all have a close method to close the connection. You just need to choose a different class for your DBMS and you can use it, but what database will my user use? I don't know. What I want is to modify the code as little as possible to meet his needs. I can abstract the following interfaces:
Package org.bromon.test; public interface DB {java.sql.Connection openDB (String url,String user,String password); void close ();}
This interface defines only two methods, without any meaningful code, which is given by the class that implements the interface, such as Mysql.java:
Package org.bromon.test; import java.sql.*; public class Mysql implements DB {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}}
Similar, of course, are Oracle.java, and so on. Interface DB classifies these classes as classes, and we define objects like this in the application:
Org.bromon.test.DB myDB
Using myDB to manipulate the database, you can ignore which class I'm actually using, which is the so-called "open-close" principle. But the problem is that the interface cannot be instantiated, myDB=new DB (), such code is absolutely wrong, we can only myDB=new Mysql () or myDB=new Oracle (). Please, I still need to specify which class is instantiated, using the interface as useless. So we need a factory:
Package org.bromon.test; public class DBFactory {public static DB Connection getConn () {Return (new Mysql ());}}
So the instantiated code becomes:
MyDB=DBFactory.getConn ()
This is the most basic general factory (Factory) of the 23 modes. The factory class is responsible for instantiating which class, while the other program logic operates on the interface DB, which is "programming against the interface". The responsibility is passed on to the factory class, but of course you can continue to define the factory interface and continue to throw the responsibility up, which becomes an abstract factory (Abstract Factory).
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.
Needless to say, the concept of inheritance is easy to understand. Why inherit it? Because you want to reuse the code? This is definitely not a reason, and the meaning of inheritance is abstraction, not code reuse. If object A has a run () method, object B wants to have the same method, so someone Class B extends A. This is a mindless approach. If you instantiate an An in B and call A's Run () method, can you achieve the same goal? As follows:
Class B {An a=new A (); a.run ();}
This is the use of class aggregation to reuse code, is the prototype of the delegation pattern, is the practice that GoF has always advocated.
Thank you for your reading, the above is the content of "how to define interface and inheritance in Java". After the study of this article, I believe you have a deeper understanding of how to define interface and inheritance in Java. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.