java中什么是Interface接口
1.Interface接口的定義和用法
先直接上大白話:馬克-to-win:接口就是灰?;页3橄蟮某橄箢悾覀兛梢跃拖裼贸橄箢愐粯佑媒涌?,只不過(guò),interface抽象到不能再抽象了,以至于里面不能有任何方法的實(shí)現(xiàn), 只能都是空方法。緊接著來(lái)個(gè)例子:
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
例1.1---本章源碼
interface OpenClose {
void open();
void close();
}
class Shop_mark_to_win implements OpenClose {
public void open() {
System.out.println("商店開門了---shop open");
}
public void close() {
System.out.println("商店關(guān)門了---shop close");
}
}
class Bottle_mark_to_win implements OpenClose {
public void open() {
System.out.println("打開瓶子,Open the Bottle");
}
public void close() {
System.out.println("蓋上瓶子Close the Bottle");
}
}
public class Test {
public static void main(String args[]) {
OpenClose s = new Shop_mark_to_win();
s.open();
s.close();
OpenClose b = new Bottle_mark_to_win();
b.open();
b.close();
System.out.println("-----------------");
OpenClose[] x = { s, b };
for (int i = 0; i < x.length; i++) {
x[i].open();
x[i].close();
}
}
}
結(jié)果是:
商店開門了---shop open
商店關(guān)門了---shop close
打開瓶子,Open the Bottle
蓋上瓶子Close the Bottle
-----------------
商店開門了---shop open
商店關(guān)門了---shop close
打開瓶子,Open the Bottle
蓋上瓶子Close the Bottle