「Java」インタフェース(interface)のfinal staticパラメータを使う方法

構文
パラメータB(実装クラス内) =インタフェース名.パラメータA(final型)
使用例
1.インタフェースの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.java.it;
public interface SampleA {
//final static パラメータの定義
public final static String strPlit = "-";
}
package com.arkgame.study.java.it; public interface SampleA { //final static パラメータの定義 public final static String strPlit = "-"; }
package com.arkgame.study.java.it;

public interface SampleA {

   //final static パラメータの定義
      public final static String strPlit = "-";

}

2.インタフェースのパラメータを利用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.java.it;
public class SampleB implements SampleA {
private static String dest = "A001-B002-C003";
public static void main(String[] args) {
String[] resArr;
<span style="color: #0000ff;"> // interface SampleAのパラメータを使う</span>
resArr = dest.split(SampleA.strPlit);
for (String tt : resArr) {
System.out.println(tt);
}
}
}
package com.arkgame.study.java.it; public class SampleB implements SampleA { private static String dest = "A001-B002-C003"; public static void main(String[] args) { String[] resArr; <span style="color: #0000ff;"> // interface SampleAのパラメータを使う</span> resArr = dest.split(SampleA.strPlit); for (String tt : resArr) { System.out.println(tt); } } }
package com.arkgame.study.java.it;

public class SampleB implements SampleA {

      private static String dest = "A001-B002-C003";

      public static void main(String[] args) {
            String[] resArr;
            // interface SampleAのパラメータを使う
            resArr = dest.split(SampleA.strPlit);
            for (String tt : resArr) {
                  System.out.println(tt);
            }

      }

}

実行結果
A001
B002
C003

Java

Posted by arkgame