// ポリモーフィズムの確認のために、入力した文字列の m 番目の文字と n 番目の文字を出力するクラス import java.io.*; import java.lang.*; public class SearchCharProcess { public static void main(String[] args) { // StringModification を継承した SearchChar クラスのオブジェクトを作成 SearchChar str = new SearchChar(); String input, indexStr, message; int len, m, n; try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("ポリモーフィズムの確認をするために、入力した文字列のm番目の文字とn番目の文字を出力します。"); System.out.println("文字列を1つ入力してください。"); System.out.print("文字列> "); input = br.readLine(); // 文字列を 1 つ入力 System.out.println("調べたいインデックスmを入力してください。"); System.out.print("m> "); indexStr = br.readLine(); // インデックス m を入力 m = Integer.parseInt(indexStr); System.out.println("調べたいインデックスnを入力してください。"); System.out.print("n> "); indexStr = br.readLine(); // インデックス n を入力 n = Integer.parseInt(indexStr); // 入力された文字列 input を SearchChar クラスのオブジェクトのフィールド変数に設定 // text は、SearchChar クラスが StringModification から継承したフィールド str.text = input; // 文字列の m 番目の文字と n 番目の文字を求め、メッセージを作成 // substring メソッドは、SearchChar クラスでオーバーライドしたもの(もとは StringModification クラスで定義されているメソッド) message = str.substring(m, n); System.out.println(message); System.out.println("文字列の長さは、オーバーライドされたメソッドを使って求められています。"); } catch(IOException e) { } } }