1. 程式人生 > >java中接口interface可以持有多個類的共享常量

java中接口interface可以持有多個類的共享常量

功能 共享 get 引入 ava strong www 常量 ring

3.接口持有多個類的共享常量 (視頻下載) (全部書籍)

接口另一主要功能,馬克-to-win: 可以使用接口來引入多個類的共享常量。所有的這些變量名都將作為常量看待。所有定義在接口中的常量都默認為public、static和final。原因見後面。

下面的例子當中,如果Server回答的結果是0或1,程序可讀性太差,效果絕對沒有YES或NO好。所以就把YES和NO放到了Client和Server的共同的接口ConstantbaseM_to_win當中。

note: the following example class Client and Server use the constants like NO and Yes at the same time, so, they define the constants in the interface of ConstantbaseM_to_win.

例1.3:

interface ConstantbaseM_to_win {
static int NO = 0;
int YES = 1;
}
class Server implements ConstantbaseM_to_win {
int answer() {
//通過其他渠道知道,今天不下雨
return NO;
}
}
class Client implements ConstantbaseM_to_win {
static void ask(int result) {
switch (result) {
case NO:
System.out.println("不下雨");
break;
case YES:
System.out.println("下雨");
break;
}
}
}
public class Test {
public static void main(String args[]) {
Client c = new Client();
。。。。。。。。。。。。。。。。。
詳情請進:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner4_web.html#InterfaceHoldConstants

java中接口interface可以持有多個類的共享常量