Connection、Statement 等预定义接口的抽象方法如何在没有主体的情况下执行某些任务?

发布于 2024-12-10 08:38:11 字数 924 浏览 0 评论 0原文

java中有很多预定义的接口,如ResultSetConnectionStatement等。接口只能有抽象方法(未实现的方法)。那么为什么我们是否可以在不先定义它们的情况下使用它们?

例如,在下面的 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();}}

,我们调用 ConnectionStatementcreateStatement()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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌︼了一个春 2024-12-17 08:38:12

调用:

System.out.println(con.getClass());

并注意实际对象不是 Connection 类型。这称为多态性。简而言之,DriverManager.getConnection() 返回实现 Connection某物。您正在调用该某物上的方法。这是多态性的一个优点 - JDBC 驱动程序稍后可以决定更改某些内容,并且只要它实现了 Connection,您的代码就无法更改少关心。

简单的示例:

List<String> list = new ArrayList<String>();
list.size();  //calling abstract method?!? No!

在上面的示例中,我们实际上调用了 ArrayList.size()。

Call:

System.out.println(con.getClass());

and notice that the actual object is not of Connection type. This is called polymorphism. Simply put DriverManager.getConnection() returns something that implements Connection. 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 implements Connection, your code couldn't care less.

Straightforward example:

List<String> list = new ArrayList<String>();
list.size();  //calling abstract method?!? No!

In the example above we are actually calling ArrayList.size().

朕就是辣么酷 2024-12-17 08:38:12

当您调用 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 of org.postgresql.jdbc4.Jdbc4Connection, which is a class that implements the java.sql.Connection interface.

Jdbc4Connection has a method body for createStatement that returns an instance of org.postgresql.jdbc4.Jdbc4Statement, which is a class that implements the java.sql.Statement interface.

Jdbc4Statement has a method body for executeQuery which returns an instance of org.postgresql.jdbc4.Jdbc4ResultSet, which is a class that implements the java.sql.ResultSet interface.

二智少女 2024-12-17 08:38:12

您正在调用实现此方法的类的方法。

DriverManager.getConnection 返回 Connection 接口的具体实现,并且您不需要知道返回的具体类。它的主要思想是拥有接口。

You're calling methods of classes that are implementing this methods.

DriverManager.getConnection returns you concrete implementation of Connection interface, and you don't need to know what exact class is returned. It's main idea of having interfaces.

梦醒时光 2024-12-17 08:38:12

当您实际调用某些东西时,您所拥有的引用就是提供所有主体的具体子类的实例。关键是您(作为客户端)不需要关心该实现是什么 - 它是由 DriverManager (等)为您提供的。您需要关心的是它是“实现给定接口的东西”或“扩展抽象类的东西”。

一个更简单的示例是使用集合类型:

List<String> list = new ArrayList<String>();
doSomething(list);

...

public static void doSomething(List<String> list) {
    // Act on the list
}

这里,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:

List<String> list = new ArrayList<String>();
doSomething(list);

...

public static void doSomething(List<String> list) {
    // Act on the list
}

Here, doSomething doesn't need to know that we used an ArrayList<E> - it could operate on any kind of List<String>... but as you can see, as it happens we create an instance of ArrayList<E>, which is a concrete class.

债姬 2024-12-17 08:38:12

您正在使用的接口的特定实现(在本例中为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文