/** * ドメイン非依存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 ポリモーフィズム(インスタンス型の確認) 017 解答例 | Javaドリル

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

015-017 015 ポリモーフィズム
class Shape {
    public void draw() {
        System.out.println("Drawing a shape");
    }
}

class Circle extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

class Rectangle extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class Main {
    public static void main(String[] args) {
        // 動的な配列を作成し、異なる形状のオブジェクトを格納
        Shape[] shapes = new Shape[3];
        shapes[0] = new Circle();
        shapes[1] = new Rectangle();
        shapes[2] = new Shape(); // Shapeも格納可能

        // 各オブジェクトがどのクラスのインスタンスかを確認
        for (Shape shape : shapes) {
            if (shape instanceof Circle) {
                System.out.println("This is an instance of Circle");
            } else if (shape instanceof Rectangle) {
                System.out.println("This is an instance of Rectangle");
            } else if (shape instanceof Shape) {
                System.out.println("This is an instance of Shape");
            }
        }
    }
}

このプログラムでは、instanceof 演算子を使用して、各オブジェクトがどのクラスのインスタンスであるかを確認しています。

出力結果:

This is an instance of Circle
This is an instance of Rectangle
This is an instance of Shape

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

🎯 実習で理解を深めよう

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

コメント

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