/** * ドメイン非依存URL出力フィルタ * javadrill.tech移行時はwp_options.home/siteurlのみ変更すればよい * * データベースには絶対URL(https://minner.asia)を保持し、 * 表示時に現在のドメイン(home_url())に動的変換する */ function javadrill_make_urls_dynamic($content) { if (empty($content)) { return $content; } // データベース内の絶対URLを現在のhome_url()に置換 $old_url = 'https://minner.asia'; $new_url = untrailingslashit(home_url()); // http版も対応(念のため) $content = str_replace($old_url, $new_url, $content); $content = str_replace('http://minner.asia', $new_url, $content); return $content; } // 投稿本文、ウィジェット、タームの説明、抜粋に適用 add_filter('the_content', 'javadrill_make_urls_dynamic', 20); add_filter('widget_text', 'javadrill_make_urls_dynamic', 20); add_filter('term_description', 'javadrill_make_urls_dynamic', 20); add_filter('get_the_excerpt', 'javadrill_make_urls_dynamic', 20); 015 ポリモーフィズム(インスタンス型の確認) 018 解答例 | Javaドリル

015 ポリモーフィズム(インスタンス型の確認) 018 解答例

015-018 015 ポリモーフィズム
// Person クラスの定義
class Person {
    // introduce メソッド
    public void introduce() {
        System.out.println("I am a person.");
    }
}

// Student クラスの定義(Person クラスを継承)
class Student extends Person {
    // introduce メソッドをオーバーライド
    @Override
    public void introduce() {
        System.out.println("I am a student.");
    }
}

// Teacher クラスの定義(Person クラスを継承)
class Teacher extends Person {
    // introduce メソッドをオーバーライド
    @Override
    public void introduce() {
        System.out.println("I am a teacher.");
    }
}

// メインクラス
public class Main {
    public static void main(String[] args) {
        // Person クラス型の動的な配列を作成
        Person[] people = new Person[3];

        // 異なる人物のオブジェクトを配列に格納
        people[0] = new Person();
        people[1] = new Student();
        people[2] = new Teacher();

        // 配列をイテレートして各オブジェクトがどのクラスのインスタンスであるかを確認
        for (Person person : people) {
            // Person クラスのインスタンスであるかを確認
            if (person instanceof Person) {
                System.out.println("This is an instance of Person");
            }
            // Student クラスのインスタンスであるかを確認
            if (person instanceof Student) {
                System.out.println("This is an instance of Student");
            }
            // Teacher クラスのインスタンスであるかを確認
            if (person instanceof Teacher) {
                System.out.println("This is an instance of Teacher");
            }

            // 各オブジェクトの introduce メソッドを呼び出し
            person.introduce();

            // 区切りの出力
            System.out.println("----------------------");
        }
    }
}

このプログラムでは、Personクラス型の動的な配列を作成し、その中に異なる人物のオブジェクトを格納しています。そして、配列をイテレートして各オブジェクトがどのクラスのインスタンスであるかを確認し、introduceメソッドを呼び出しています。

実行結果:

This is an instance of Person
I am a person.
----------------------
This is an instance of Person
This is an instance of Student
I am a student.
----------------------
This is an instance of Person
This is an instance of Teacher
I am a teacher.
----------------------

「015 ポリモーフィズム」問題集リスト

🎯 実習で理解を深めよう

この内容についてJavaDrillで実際に手を動かして学習できます

コメント

タイトルとURLをコピーしました