Test.java
class Test {
public static void main(String[] args) {
System.out.println("Test");
}
}
bash
>javac Test.java
>java Test
Test
Index.java
package test;
public class Index {
public static void main(String[] args) {
TokyoService tokyo = new TokyoService();
for (String city : tokyo.getCities()) {
System.out.println(city);
}
OsakaService osaka = new OsakaService();
System.out.println(osaka.getCity(0));
}
}
IPrefecture
package test;
public interface IPrefecture {
String[] getCities();
// オーバーロード
String getCity(int id);
}
TokyoService
package test;
public class TokyoService implements IPrefecture {
static final String[] cities = {"中央区", "浪速区"};
public TokyoService() {
System.out.println("Osaka");
}
@Override
public String[] getCities() {
return cities;
}
@Override
public String getCity(int id) {
return cities[id];
}
}
OsakaService
package test;
public class OsakaService implements IPrefecture {
static final String[] cities = {"渋谷区", "新宿区"};
public OsakaService() {
System.out.println("Osaka");
}
@Override
public String[] getCities() {
return cities;
}
@Override
public String getCity(int id) {
return cities[id];
}
}