/** * ドメイン非依存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 ポリモーフィズム(アップキャスト) 007 解答例 | Javaドリル

015 ポリモーフィズム(アップキャスト) 007 解答例

015-007 015 ポリモーフィズム
class Instrument {
    void play() {
        System.out.println("Playing an instrument");
    }
}

class Piano extends Instrument {
    void play() {
        System.out.println("Playing the piano");
    }
}

public class Main {
    public static void main(String[] args) {
        // Instrument クラスのオブジェクトを作成
        Instrument instrument = new Instrument();
        // Instrument クラスの play メソッドを呼び出す
        instrument.play();

        // Piano クラスのオブジェクトを作成
        Piano piano = new Piano();
        // Piano クラスの play メソッドを呼び出す(オーバーライドされたバージョンが呼ばれる)
        piano.play();

        // アップキャストを使用して Instrument クラスの参照変数で Piano クラスのオブジェクトを操作
        Instrument upcastedPiano = new Piano();
        // Instrument クラスの参照変数で Piano クラスの play メソッドを呼び出す(オーバーライドされたバージョンが呼ばれる)
        upcastedPiano.play();
    }
}

この例では、Instrument クラスと Piano クラスを使用して、メインクラスでそれぞれのクラスのオブジェクトを作成しています。そして、アップキャストを使用して Instrument クラスの参照変数で Piano クラスのオブジェクトを操作しています。

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

🎯 実習で理解を深めよう

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

コメント

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