/** * ドメイン非依存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); 018 オブジェクト指向の深化(インターフェイスと抽象クラスの利用) 005 解答例 | Javaドリル

018 オブジェクト指向の深化(インターフェイスと抽象クラスの利用) 005 解答例

018-005 018 オブジェクト指向の深化
import java.util.ArrayList;
import java.util.List;

// Drawable インターフェイス
interface Drawable {
    void draw();
}

// Circle クラス
class Circle implements Drawable {
    // メンバー変数
    private Point center; // 中心座標
    private double radius; // 半径

    // コンストラクタ
    public Circle(Point center, double radius) {
        this.center = center;
        this.radius = radius;
    }

    // draw メソッドの実装
    @Override
    public void draw() {
        System.out.println("Drawing a circle with center at " + center + " and radius " + radius);
    }
}

// Rectangle クラス
class Rectangle implements Drawable {
    // メンバー変数
    private Point topLeft; // 左上の座標
    private double width;   // å¹…
    private double height;  // 高さ

    // コンストラクタ
    public Rectangle(Point topLeft, double width, double height) {
        this.topLeft = topLeft;
        this.width = width;
        this.height = height;
    }

    // draw メソッドの実装
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle with top left corner at " + topLeft +
                ", width " + width + ", and height " + height);
    }
}

// DrawingBoard クラス
class DrawingBoard {
    // メンバー変数
    private List<Drawable> shapes;

    // コンストラクタ
    public DrawingBoard() {
        this.shapes = new ArrayList<>();
    }

    // addShape メソッドの実装
    public void addShape(Drawable shape) {
        shapes.add(shape);
    }

    // 描画ボード上のすべての図形を描画するメソッド
    public void drawAllShapes() {
        for (Drawable shape : shapes) {
            shape.draw();
        }
    }
}

// Point クラス(座標を表すクラス)
class Point {
    // メンバー変数
    private double x;
    private double y;

    // コンストラクタ
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ")";
    }
}

この解答例では、Drawable インターフェイスを実装した Circle クラスと Rectangle クラスがあります。これらの図形を DrawingBoard クラスで管理し、addShape メソッドで描画ボードに追加できます。drawAllShapes メソッドを呼び出すことで、描画ボード上のすべての図形を描画できます。

「018 オブジェクト指向の深化」問題集リスト

🎯 実習で理解を深めよう

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

コメント

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