Connection、Statement 等预定义接口的抽象方法如何在没有主体的情况下执行某些任务?
java中有很多预定义的接口,如ResultSet
、Connection
、Statement
等。接口只能有抽象方法(未实现的方法)。那么为什么我们是否可以在不先定义它们的情况下使用它们?
例如,在下面的 jdbc 代码中
public class JDBCSample {
public static void main( String args[]) {
String connectionURL = "jdbc:postgresql://localhost:5432/movies;
user=java;password=samples";`
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection (connectionURL);
Statement stmt = con.createStatement();
ResultSet rs = stmd.executeQuery("select moviename, releasedate from movies");
while (rs.next())
{....do something.....}
}catch (SQLException e)
{e.printStackTrace();}
catch (Exception e)
{ e.printStackTrace();}}
,我们调用 Connection
和 StatementcreateStatement()
和 executeQuery()
方法代码>接口?如果是,那么抽象方法(没有主体的方法)如何执行某些任务?
There are many predefined interfaces in java like ResultSet
, Connection
, Statement
etc.An Interface can have only abstract methods (unimplemented methods).So why do we use there methods without defining them first.
for example in following code of jdbc
public class JDBCSample {
public static void main( String args[]) {
String connectionURL = "jdbc:postgresql://localhost:5432/movies;
user=java;password=samples";`
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection (connectionURL);
Statement stmt = con.createStatement();
ResultSet rs = stmd.executeQuery("select moviename, releasedate from movies");
while (rs.next())
{....do something.....}
}catch (SQLException e)
{e.printStackTrace();}
catch (Exception e)
{ e.printStackTrace();}}
here are we calling abstract createStatement()
and executeQuery()
method of Connection
and Statement
interface? If yes then how an abstract method(method without body) can perform some task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
调用:
并注意实际对象不是
Connection
类型。这称为多态性。简而言之,DriverManager.getConnection()
返回实现Connection
的某物。您正在调用该某物上的方法。这是多态性的一个优点 - JDBC 驱动程序稍后可以决定更改某些内容,并且只要它实现了 Connection,您的代码就无法更改少关心。简单的示例:
在上面的示例中,我们实际上调用了 ArrayList.size()。
Call:
and notice that the actual object is not of
Connection
type. This is called polymorphism. Simply putDriverManager.getConnection()
returns something that implementsConnection
. You are invoking methods on that something. This is an advantage of polymorphism - the JDBC driver can later decide to change something and as long as it implementsConnection
, your code couldn't care less.Straightforward example:
In the example above we are actually calling
ArrayList.size()
.当您调用 DriverManager.getConnection 时,它会返回 org.postgresql.jdbc4.Jdbc4Connection 的实例,该实例是实现 java.sql.Connection< /代码> 接口。
Jdbc4Connection
有一个createStatement
的方法主体,它返回org.postgresql.jdbc4.Jdbc4Statement
的实例,该实例是一个实现>java.sql.Statement
接口。Jdbc4Statement
有一个executeQuery
的方法主体,它返回org.postgresql.jdbc4.Jdbc4ResultSet
的实例,这是一个实现>java.sql.ResultSet
接口。When you call
DriverManager.getConnection
, it returns an instance oforg.postgresql.jdbc4.Jdbc4Connection
, which is a class that implements thejava.sql.Connection
interface.Jdbc4Connection
has a method body forcreateStatement
that returns an instance oforg.postgresql.jdbc4.Jdbc4Statement
, which is a class that implements thejava.sql.Statement
interface.Jdbc4Statement
has a method body forexecuteQuery
which returns an instance oforg.postgresql.jdbc4.Jdbc4ResultSet
, which is a class that implements thejava.sql.ResultSet
interface.您正在调用实现此方法的类的方法。
DriverManager.getConnection
返回Connection
接口的具体实现,并且您不需要知道返回的具体类。它的主要思想是拥有接口。You're calling methods of classes that are implementing this methods.
DriverManager.getConnection
returns you concrete implementation ofConnection
interface, and you don't need to know what exact class is returned. It's main idea of having interfaces.当您实际调用某些东西时,您所拥有的引用就是提供所有主体的具体子类的实例。关键是您(作为客户端)不需要关心该实现是什么 - 它是由 DriverManager (等)为您提供的。您需要关心的是它是“实现给定接口的东西”或“扩展抽象类的东西”。
一个更简单的示例是使用集合类型:
这里,
doSomething
不需要知道我们使用了ArrayList
- 它可以对 进行操作任何类型的List
...但是正如你所看到的,我们碰巧创建了一个ArrayList
的实例,它是一个具体类。By the time you're actually calling something, the reference you have is to an instance of a concrete subclass which provides all the bodies. The point is that you (as the client) don't need to care what that implementation is - it's provided for you by
DriverManager
(etc). All you need to care about is that it's "something which implements the given interface" or "something which extends the abstract class".A simpler example would be to use the collection types:
Here,
doSomething
doesn't need to know that we used anArrayList<E>
- it could operate on any kind ofList<String>
... but as you can see, as it happens we create an instance ofArrayList<E>
, which is a concrete class.您正在使用的接口的特定实现(在本例中为
DriverManager
)返回一个实现该接口的抽象方法的具体类。正如您所指出的,接口仅声明具体实现必须提供的方法。实际的实现类提供了这些方法的主体。
DriverManager 类及其包提供了缺失的部分,返回具有完整方法体的具体类。但是,您只能通过接口与客户端代码端的这些类进行交互。具体类的实现细节有效地隐藏在视图之外。
The particular implementation of the interface you are using, in this case
DriverManager
, returns a concrete class that implements the abstract methods of the interface.As you pointed out, the interface only declares the methods that the concrete implementation must provide. The actual implementation class provides the bodies of the those methods.
The
DriverManager
class and its package provide the missing pieces, returning concrete classes that have complete method bodies. You interact with those classes on your client code side only through the interface, however. The implementation details of the concrete classes are effectively hidden from view.