1
0

More than 3 years have passed since last update.

Java

Last updated at Posted at 2020-01-30
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];
  }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0