English 中文(简体)
在没有机构的情况下,像连接、声明等事先界定的接口的抽象方法如何执行某些任务?
原标题:How does abstract method of predefined interface like Connection, Statement etc. perform some task without having body?

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();}}

这里,我们称之为“抽象的<代码>createStatement(和executeery()”方法:Statement接口? 如果是的话,那么抽象的方法(没有身体的方法)如何能够执行某些任务?

最佳回答

在请上<条码>的驱动器/代码<>时,请上<条码>>。

<代码>Jdbc4Connection>有一个方法机构,可检索createStatement.org.postgresql.jdbc4.Jdbc4Statement,该表格是实施<编码>java.sql.Statement接口的类别。

<>Jdbc4Statement有一个方法机构,可检索execute Query/code>,该仪器可检索org.postgresql.jdbc4.Jdbc4ResultSet,这个类别可实施java.sql.ResultSet 接口。

问题回答

呼吁:

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!

在上述例子中,我们实际上称为ArrayList.size()。

你呼吁采用这种方法的班级方法。

<代码>驱动器/代码>回收器/代码>,使您能够具体实施<代码>Connection接口,而且你无需知道哪类保单被退回。 其主要想法是建立接口。

到你重新开始问答时,你指的是一个提供所有机构的具体子级。 问题在于,你(作为客户)不需要注意执行工作,而是通过<条码>提供。 你们都需要注意的是,它“执行特定接口的麻烦”或“扩展抽象类别的一些因素”。

更简单的例子是使用收集类型:

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.

具体实施你在此情况下使用的界面 司机/编码>,将采用接口抽象方法的具体类别退回。

如你所指出的,接口仅限宣布了具体执行必须提供的方法。 实际执行类别为这些方法提供了机构。

<编码驱动器/编码>类别及其包裹提供了缺失的物品,并恢复了有完整方法体的具体班级。 不过,你只通过接口与客户代码上的这些班子互动。 具体班级的执行细节实际上不为人所知。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...