预定义接口的抽象方法如Connection,Statement等如何在没有主体的情况下执行某些任务? [英] How does abstract method of predefined interface like Connection, Statement etc. perform some task without having body?
问题描述
java中有许多预定义的接口,如 ResultSet
,连接
,声明
等。接口只能有抽象方法(未实现的方法)。所以我们为什么要使用那些方法而不先定义它们。
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.
例如在下面代码jdbc
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();}}
这里我们调用abstract createStatement()
和 executeQuery()
<$ c $的方法c>连接和声明
界面?如果是,则抽象方法(没有身体的方法)如何执行某项任务?
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?
推荐答案
当您致电 DriverManager.getConnection
,它返回 org.postgresql.jdbc4.Jdbc4Connection
的实例,这是一个实现 java.sql.Connection
interface。
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
有一个 createStatement
的方法体,它返回 org.postgresql.jdbc4.Jdbc4Statement
的一个实例,它是一个实现 java.sql.Statement
接口的类。
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
有一个 executeQuery
的方法体返回 org.postgresql.jdbc4.Jdbc4ResultSet
的实例,这是一个实现 java.sql.ResultSet
的类界面。
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.
这篇关于预定义接口的抽象方法如Connection,Statement等如何在没有主体的情况下执行某些任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!