天天看點

接口中定義變量的簡介說明

轉自:

​​http://www.java265.com/JavaJingYan/202205/16524471303360.html​​

Java接口是一系列方法的聲明

    是一些方法特征的集合

     一個接口隻有方法的特征沒有方法的實作 

     是以這些方法可以在不同的地方被不同的類實作,而這些實作可以具有不同的行為

java接口定義的變量:
       1.java接口定義的變量都是靜态變量
       2.java接口中定義的變量都預設加上 public static final關鍵字
       3.java接口中的變量可起到在多個類中共享變量的效果
例:      
interface ITest {
    static int NO = 0;
    int YES = 1;
}
class ITestImpl implements ITest {
    int testFlag() {
        return NO; 
    }
}
class Client implements ITest {
    static void show(int result) {
        switch (result) {
        case NO:
            System.out.println("輸出no");
            break;
        case YES:
            System.out.println("輸出yes");
            break;
        }
    }
}
public class Test {
    public static void main(String args[]) {
        Client c = new Client();
        ITestImpl s = new ITestImpl();
        c.show(s.testFlag());
    }
}