如上图接口类List只具有方法,无法实例化,我们在使用时必须通过下面的实现类来使用List的方法。
用一个类 implements 接口,这样这个类就可以使用接口中的方法了。
- 接口实例化
1.不用implements,直接用接口指向实现类的实现,类似于用父类指向子类的实现,比如List接口,如:java.util.List list = new ArrayList();//List为接口,ArrayList为接口的实现类。此处list可以使用List接口的方法。具体使用方法如下:
2.不用implements,通过API文档下的另请参见:方法创建抽象类对象,
2.1 比如接口Connection :
Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassWord);//Connection 为接口,DriverManager.getConnection为API文档中Connection接口下的“另外参见”这种方法很重要!
这种方法很重要!如下图
2.2 比如接口Statement
Statement stat = con.createStatement();//con 为Connection对象