1.背景介紹
接口是什么?
接口杰妓,在Java中是一個抽象類型藻治,是抽象方法的集合碘勉。接口通常以interface來聲名巷挥。一個類通過繼承接口的方式,從而來繼承接口的抽象方法验靡。接口嚴格意義上來講屬于一個特殊的類倍宾,而這個類里面只有抽象方法和全局常量,就連構(gòu)造方法也沒有胜嗓。
高职,比如我們約定某些英雄是物理系英雄,那么他們就一定能夠進行物理攻擊辞州。實現(xiàn)某個接口怔锌,就相當于承諾了某種約定。所以實現(xiàn)某個接口变过,就必須提供那個接口中的方法埃元,實現(xiàn)在語法上使用關(guān)鍵字implements
2.知識剖析
接口的特點:??
1、Java接口中的成員變量默認都是public,static,final類型的(都可省略),必須被顯示初始化,即接口中的成員變量為常量(大寫,單詞之間用"_"分隔)
2媚狰、Java接口中的方法默認都是public,abstract類型的(都可省略),沒有方法體,不能被實例化
3岛杀、Java接口中只能包含public,static,final類型的成員變量和public,abstract類型的成員方法
4、接口中沒有構(gòu)造方法,不能被實例化
5崭孤、一個接口不能實現(xiàn)(implements)另一個接口,但它可以繼承多個其它的接口
6类嗤、Java接口必須通過類來實現(xiàn)它的抽象方法
7糊肠、當類實現(xiàn)了某個Java接口時,它必須實現(xiàn)接口中的所有抽象方法,否則這個類必須聲明為抽象類
8、不允許創(chuàng)建接口的實例(實例化),但允許定義接口類型的引用變量,該引用變量引用實現(xiàn)了這個接口的類的實例
9遗锣、一個類只能繼承一個直接的父類,但可以實現(xiàn)多個接口,間接的實現(xiàn)了多繼承.
Interface和Impl這種方式的好處是什么货裹?
便于封裝;降低耦合精偿;對修改封閉泪酱,對拓展開放。
3.常見問題
接口使用原則:
? ?1)接口必須要有子類还最,但此時一個子類可以使用implements關(guān)鍵字實現(xiàn)多個接口
2)接口的子類(如果不是抽象類)墓阀,那么必須要覆蓋重寫接口中的全部抽象方法
3)接口的對象可以利用子類對象的向上轉(zhuǎn)型進行實例化
4.擴展思考討論
接口和抽象類的區(qū)別?
一個抽象類只能繼承一個抽象父類拓轻,而接口可以繼承多個接口
一個子類只能繼承一個抽象類斯撮,卻可以實現(xiàn)多個接口
接口實現(xiàn)的基礎——對象轉(zhuǎn)型和多態(tài)
對象轉(zhuǎn)型
package?fourth.com;
public class?Quadrangle?{
public static void?draw(Quadrangle?q){
System.out.println("hello world");
? ?}
}
package?fourth.com;
public class?Parallelogram?extends?Quadrangle{
public static void?main(String?args[]){
Parallelogram?p?=?new?Parallelogram();//這就相當于"Quadrangle obj=new Parallelogram()",就是把子類對象賦值給父類類型的變量扶叉,這就是向上轉(zhuǎn)型
? ? ? ?draw(p);
? ?}
}
多態(tài)
package?fourth.com;
public class?Second?{
private?Second[]?qtest?=?new?Second[6];
private int?nextIndex?=?0;
public void?draw(Second?q){
if(nextIndex
qtest[nextIndex] = q;
? ? ? ? ? ?System.out.println(nextIndex);
? ? ? ? ? ?nextIndex++;
? ? ? ?}
}
public static void?main(String[] args) {
Second?q?=?new?Second();
? ? ? ?q.draw(new?Third());
? ? ? ?q.draw(new?Fourth());
? ?}
}
package?fourth.com;
public class?Third?extends?Second{
public?Third(){
System.out.println("正方形");
? ?}
}
package?fourth.com;
public class?Fourth?extends?Second{
public?Fourth(){
System.out.println("平行四邊形");
? ?}
}
5.編碼實戰(zhàn)
package?fourth.Dao;
import?fourth.com.student;
public interface?studentDao?{
public int?addStudent(student?student);
public int?updateStudent(String?name);
public int?deleteStudent(String?name);
public?student?findStudent(student?student);
}
package?fourth.imple;
import?fourth.com.*;
import?fourth.Dao.studentDao;
import?com.mysql.jdbc.Connection;
import?com.mysql.jdbc.PreparedStatement;
import?java.sql.ResultSet;
public class?createStudent?implements?studentDao{
@Override
? ?public int?addStudent(student?student) {
Connection?conn?=?null;
? ? ? ?PreparedStatement?ps?=?null;
int?i=?0;
try?{
conn?= (Connection)?mysqlConnect.getconnection();
? ? ? ? ? ?String?sql?=?"insert into student values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
? ? ? ? ? ?ps?= (PreparedStatement)?conn.prepareStatement(sql);
? ? ? ? ? ?ps.setInt(1,student.getId());
? ? ? ? ? ?ps.setString(2,student.getCreate_at());
? ? ? ? ? ?ps.setString(3,student.getUpdate_at());
? ? ? ? ? ?ps.setString(4,student.getName());
? ? ? ? ? ?ps.setString(5,student.getDailyLink());
? ? ? ? ? ?ps.setInt(6,student.getQQ());
? ? ? ? ? ?ps.setString(7,student.getOnlineNumber());
? ? ? ? ? ?ps.setString(8,student.getMail());
? ? ? ? ? ?ps.setInt(9,student.getPhone());
? ? ? ? ? ?ps.setString(10,student.getEnrollmentTime());
? ? ? ? ? ?ps.setString(11,student.getProfessionType());
? ? ? ? ? ?ps.setString(12,student.getBrotherName());
? ? ? ? ? ?ps.setString(13,student.getPromise());
? ? ? ? ? ?i?=?ps.executeUpdate();
? ? ? ?}catch?(Exception?e){
e.printStackTrace();
? ? ? ?}finally?{
mysqlConnect.free(null,ps,?conn);
? ? ? ? ? ?System.out.println("close mysqlConnection");
? ? ? ?}
return?i;
? ?}
@Override
? ?public int?updateStudent(String?name) {
Connection?conn?=?null;
? ? ? ?PreparedStatement?ps?=?null;
int?i?=?0;
try?{
conn?= (Connection)?mysqlConnect.getconnection();
? ? ? ? ? ?String?sql?=?"update student set promise = '一姐最胖' where name=?";
? ? ? ? ? ?ps?= (PreparedStatement)?conn.prepareStatement(sql);
? ? ? ? ? ?ps.setString(1,name);
? ? ? ? ? ?i?=?ps.executeUpdate();
? ? ? ?}catch?(Exception?e){
e.printStackTrace();
? ? ? ?}finally?{
mysqlConnect.free(null,ps,?conn);
? ? ? ? ? ?System.out.println("close mysqlConnection");
? ? ? ?}
return?i;
? ?}
@Override
? ?public int?deleteStudent(String?name) {
Connection?conn?=?null;
? ? ? ?PreparedStatement?ps?=?null;
int?i?=?0;
try?{
conn?= (Connection)?mysqlConnect.getconnection();
? ? ? ? ? ?String?sql?=?"delete from student where name=?";
? ? ? ? ? ?ps?= (PreparedStatement)?conn.prepareStatement(sql);
? ? ? ? ? ?ps.setString(1,name);
? ? ? ? ? ?i?=?ps.executeUpdate();
? ? ? ?}catch?(Exception?e){
e.printStackTrace();
? ? ? ?}finally?{
mysqlConnect.free(null,ps,conn);
? ? ? ? ? ?System.out.println("close mysqlConnection");
? ? ? ?}
return?i;
? ?}
@Override
? ?public?student?findStudent(student?student) {
Connection?conn?=?null;
? ? ? ?PreparedStatement?ps?=?null;
? ? ? ?student?stu?=?null;
? ? ? ?ResultSet?rs?=?null;
try?{
conn?= (Connection)?mysqlConnect.getconnection();
? ? ? ? ? ?String?sql?=?"select?*?from student where ID=? ";
? ? ? ? ? ?ps?= (PreparedStatement)?conn.prepareStatement(sql);
? ? ? ? ? ?ps.setInt(1,student.getId());
? ? ? ? ? ?rs?=?ps.executeQuery()?;
? ? ? ? ? ?stu?=?new?student();
while?(rs.next()){
stu.setId(rs.getInt(1));
? ? ? ? ? ? ? ?stu.setCreate_at(rs.getString(2));
? ? ? ? ? ? ? ?stu.setUpdate_at(rs.getString(3));
? ? ? ? ? ? ? ?stu.setName(rs.getString(4));
? ? ? ? ? ? ? ?stu.setDailyLink(rs.getString(5));
? ? ? ? ? ? ? ?stu.setQQ(rs.getInt(6));
? ? ? ? ? ? ? ?stu.setOnlineNumber(rs.getString(7));
? ? ? ? ? ? ? ?stu.setMail(rs.getString(8));
? ? ? ? ? ? ? ?stu.setPhone(rs.getInt(9));
? ? ? ? ? ? ? ?stu.setEnrollmentTime(rs.getString(10));
? ? ? ? ? ? ? ?stu.setProfessionType(rs.getString(11));
? ? ? ? ? ? ? ?stu.setBrotherName(rs.getString(12));
? ? ? ? ? ? ? ?stu.setPromise(rs.getString(13));
? ? ? ? ? ?}
}catch?(Exception?e){
e.printStackTrace();
? ? ? ?}finally?{
mysqlConnect.free(null,ps,conn);
? ? ? ?}
return?student;
? ?}
}
6更多討論
1)接口有什么修飾符勿锅?
接口就是提供一種統(tǒng)一的”協(xié)議”,而接口中的屬性也屬于“協(xié)議”中的成員枣氧。它們是公共的溢十,靜態(tài)的,最終的常量达吞。相當于全局常量张弛。抽象類是不“完全”的類,相當于是接口和具體類的一個中間層酪劫。即滿足接口的抽象吞鸭,也滿足具體的實現(xiàn)。
接口用于描述系統(tǒng)對外提供的所有服務,因此接口中的成員常量和方法都必須是公開(public)類型的,確保外部使用者能訪問它們覆糟;
接口僅僅描述系統(tǒng)能做什么,但不指明如何去做,所以接口中的方法都是抽象(abstract)方法刻剥;
接口不涉及和任何具體實例相關(guān)的細節(jié),因此接口沒有構(gòu)造方法,不能被實例化,沒有實例變量,只有靜態(tài)(static)變量滩字。
接口的中的變量是所有實現(xiàn)類共有的造虏,既然共有,肯定是不變的東西麦箍,因為變化的東西也不能夠算共有漓藕。所以變量是不可變(final)類型,也就是常量了内列。
2)為什么接口中的方法修飾符是public而不是static撵术?
接口用于描述系統(tǒng)對外提供的所有服務,因此接口中的成員常量和方法都必須是公開(public)類型的,確保外部使用者能訪問它們;
抽象類中的抽象方法(前面有abstract修飾)不能用private,static,修飾话瞧。原因:抽象方法沒有方法體嫩与,是被用來繼承的寝姿,所以不能用private修飾;static修飾的方法可以通過類名來訪問該方法(即方法的方法體)划滋,抽象方法用static沒有意義饵筑。接口是一種特殊的抽象類,所以用public而不是static
3)接口中可不可以有方法體处坪?
Java8之前的版本中接口不能寫方法體根资,但是在Java8開始就可以寫了,使用default關(guān)鍵字