java中請給一個(gè)Abstract類實(shí)現(xiàn)接口的實(shí)例
2.Abstract類實(shí)現(xiàn)接口
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號: 73203。
馬克-to-win:如果實(shí)現(xiàn)某接口的類是abstract類,則它可以不實(shí)現(xiàn)該接口所有的方法。但其非abstract的子類中必須擁有所有抽象方法的實(shí)在的方法體;(當(dāng)然它abstract爹的也算作是它的)
If
a class implements an interface, it must implement all of its methods
in the interface, otherwise, this class must be an abstract class. if
it is an abstract class, it can leave some methods in the interface
unimplemented.refer to the following example.
例1.2---本章源碼
interface OpenClose {
void open();
void close();
}
abstract class Door implements OpenClose {
public void close() {
System.out.println("旋轉(zhuǎn)把手,拉!");
}
}
/*AdvancedDoorMark_to_win這個(gè)類不需要實(shí)現(xiàn)close()。因?yàn)樗呀?jīng)有close()。它的close()位置在它的超類"Door"。
AdvancedDoorMark_to_win does not need to implement close(), because it already has
close(), the only thing is that the position of its close() is inside its
super class "Door"
*/
class AdvancedDoorMark_to_win extends Door {
public void open() {
System.out.println("旋轉(zhuǎn)把手,推!");
}
}
public class Test {
public static void main(String args[]) {
AdvancedDoorMark_to_win d = new AdvancedDoorMark_to_win();
d.open();
d.close();
}
}
輸出結(jié)果:
旋轉(zhuǎn)把手,推!
旋轉(zhuǎn)把手,拉!