Skip to main content
本文から 1 文字削除
ソース リンク
BLUEPIXY
  • 6,907
  • 1
  • 21
  • 34
class Circle{
    double x;
    double y;
    double radius;`;

    Circle(double radius){
        this.radius = radius;
    }
    Circle(double x, double y, double radius){
        this.x      = x;
        this.y      = y;
        this.radius = radius;
    }
    void move_x(double x){
        this.x = x;
    }
    void move_x(double x , double y){
        this.x = x;
        this.y = y;
    }
    void move_y(double y){
        this.y = y;
    }
    void move_y(double x,double y){
        this.x = x;
        this.y = y;
    }
    void scale(double a){
        radius *= a;
    }}

class CircleOverloadMethod{
public static void main(String args[]){
    Circle c = new Circle(4);
    Circle d = new Circle(4);
    System.out.println("x軸上で移動");
    c.move_x(2);
    c.scale(0.5);
    System.out.println("c.x      = " + c.x);
    System.out.println("c.y      = " + c.y);
    System.out.println("c.radius = " + c.radius);
    c.move_x(-2,-2);
    c.scale(2);
    System.out.println("c.x      =" + c.x);
    System.out.println("c.y      =" + c.y);
    System.out.println("c.radius =" + c.radius);
    System.out.println("y軸上で移動");
    d.move_y(2);
    d.scale(0.5);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);
    d.move_y(-4,-4);
    d.scale(4);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);

}}
class Circle{
    double x;
    double y;
    double radius;`

    Circle(double radius){
        this.radius = radius;
    }
    Circle(double x, double y, double radius){
        this.x      = x;
        this.y      = y;
        this.radius = radius;
    }
    void move_x(double x){
        this.x = x;
    }
    void move_x(double x , double y){
        this.x = x;
        this.y = y;
    }
    void move_y(double y){
        this.y = y;
    }
    void move_y(double x,double y){
        this.x = x;
        this.y = y;
    }
    void scale(double a){
        radius *= a;
    }}

class CircleOverloadMethod{
public static void main(String args[]){
    Circle c = new Circle(4);
    Circle d = new Circle(4);
    System.out.println("x軸上で移動");
    c.move_x(2);
    c.scale(0.5);
    System.out.println("c.x      = " + c.x);
    System.out.println("c.y      = " + c.y);
    System.out.println("c.radius = " + c.radius);
    c.move_x(-2,-2);
    c.scale(2);
    System.out.println("c.x      =" + c.x);
    System.out.println("c.y      =" + c.y);
    System.out.println("c.radius =" + c.radius);
    System.out.println("y軸上で移動");
    d.move_y(2);
    d.scale(0.5);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);
    d.move_y(-4,-4);
    d.scale(4);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);

}}
class Circle{
    double x;
    double y;
    double radius;

    Circle(double radius){
        this.radius = radius;
    }
    Circle(double x, double y, double radius){
        this.x      = x;
        this.y      = y;
        this.radius = radius;
    }
    void move_x(double x){
        this.x = x;
    }
    void move_x(double x , double y){
        this.x = x;
        this.y = y;
    }
    void move_y(double y){
        this.y = y;
    }
    void move_y(double x,double y){
        this.x = x;
        this.y = y;
    }
    void scale(double a){
        radius *= a;
    }}

class CircleOverloadMethod{
public static void main(String args[]){
    Circle c = new Circle(4);
    Circle d = new Circle(4);
    System.out.println("x軸上で移動");
    c.move_x(2);
    c.scale(0.5);
    System.out.println("c.x      = " + c.x);
    System.out.println("c.y      = " + c.y);
    System.out.println("c.radius = " + c.radius);
    c.move_x(-2,-2);
    c.scale(2);
    System.out.println("c.x      =" + c.x);
    System.out.println("c.y      =" + c.y);
    System.out.println("c.radius =" + c.radius);
    System.out.println("y軸上で移動");
    d.move_y(2);
    d.scale(0.5);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);
    d.move_y(-4,-4);
    d.scale(4);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);

}}
本文に 120 文字追加
ソース リンク
BLUEPIXY
  • 6,907
  • 1
  • 21
  • 34

自分でやってみたコードは
class Circle{ double x; double y; double radius;

class Circle{
    double x;
    double y;
    double radius;`

    Circle(double radius){
        this.radius = radius;
    }
    Circle(double x, double y, double radius){
        this.x      = x;
        this.y      = y;
        this.radius = radius;
    }
    void move_x(double x){
        this.x = x;
    }
    void move_x(double x , double y){
        this.x = x;
        this.y = y;
    }
    void move_y(double y){
        this.y = y;
    }
    void move_y(double x,double y){
        this.x = x;
        this.y = y;
    }
    void scale(double a){
        radius *= a;
    }}

`

 
class CircleOverloadMethod{
public static void main(String args[]){
    Circle c = new Circle(4);
    Circle d = new Circle(4);
    System.out.println("x軸上で移動");
    c.move_x(2);
    c.scale(0.5);
    System.out.println("c.x      = " + c.x);
    System.out.println("c.y      = " + c.y);
    System.out.println("c.radius = " + c.radius);
    c.move_x(-2,-2);
    c.scale(2);
    System.out.println("c.x      =" + c.x);
    System.out.println("c.y      =" + c.y);
    System.out.println("c.radius =" + c.radius);
    System.out.println("y軸上で移動");
    d.move_y(2);
    d.scale(0.5);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);
    d.move_y(-4,-4);
    d.scale(4);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);

}}

自分でやってみたコードは
class Circle{ double x; double y; double radius;

Circle(double radius){
    this.radius = radius;
}
Circle(double x, double y, double radius){
    this.x      = x;
    this.y      = y;
    this.radius = radius;
}
void move_x(double x){
    this.x = x;
}
void move_x(double x , double y){
    this.x = x;
    this.y = y;
}
void move_y(double y){
    this.y = y;
}
void move_y(double x,double y){
    this.x = x;
    this.y = y;
}
void scale(double a){
    radius *= a;
}}

`

class CircleOverloadMethod{
public static void main(String args[]){
    Circle c = new Circle(4);
    Circle d = new Circle(4);
    System.out.println("x軸上で移動");
    c.move_x(2);
    c.scale(0.5);
    System.out.println("c.x      = " + c.x);
    System.out.println("c.y      = " + c.y);
    System.out.println("c.radius = " + c.radius);
    c.move_x(-2,-2);
    c.scale(2);
    System.out.println("c.x      =" + c.x);
    System.out.println("c.y      =" + c.y);
    System.out.println("c.radius =" + c.radius);
    System.out.println("y軸上で移動");
    d.move_y(2);
    d.scale(0.5);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);
    d.move_y(-4,-4);
    d.scale(4);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);

}}

自分でやってみたコードは

class Circle{
    double x;
    double y;
    double radius;`

    Circle(double radius){
        this.radius = radius;
    }
    Circle(double x, double y, double radius){
        this.x      = x;
        this.y      = y;
        this.radius = radius;
    }
    void move_x(double x){
        this.x = x;
    }
    void move_x(double x , double y){
        this.x = x;
        this.y = y;
    }
    void move_y(double y){
        this.y = y;
    }
    void move_y(double x,double y){
        this.x = x;
        this.y = y;
    }
    void scale(double a){
        radius *= a;
    }}
 
class CircleOverloadMethod{
public static void main(String args[]){
    Circle c = new Circle(4);
    Circle d = new Circle(4);
    System.out.println("x軸上で移動");
    c.move_x(2);
    c.scale(0.5);
    System.out.println("c.x      = " + c.x);
    System.out.println("c.y      = " + c.y);
    System.out.println("c.radius = " + c.radius);
    c.move_x(-2,-2);
    c.scale(2);
    System.out.println("c.x      =" + c.x);
    System.out.println("c.y      =" + c.y);
    System.out.println("c.radius =" + c.radius);
    System.out.println("y軸上で移動");
    d.move_y(2);
    d.scale(0.5);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);
    d.move_y(-4,-4);
    d.scale(4);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);

}}
ソース リンク
user10197
  • 83
  • 1
  • 1
  • 11

プログラミングでCircleクラスのmove()メソッドをオーバーロードする方法について

独習でjavaを学習中なのですが、そこで
”1つ目のmove()メソッドでは円をx軸上で移動します。2つ目のメソッドでは中心座標を両方とも更新します。
double型の引数を1つだけ受け取って円をy軸上に移動するようなmove()メソッドは作成できません。そのようなメソッドは一つ目のメソッドとシグネチャが同じになりコンパイルエラーとなるためです"
と書かれているのですが
そこで自分なりにy軸上に移動するようなメソッドを作成してみたのですがエラーとなりません。

自分でやってみたコードは
class Circle{ double x; double y; double radius;

Circle(double radius){
    this.radius = radius;
}
Circle(double x, double y, double radius){
    this.x      = x;
    this.y      = y;
    this.radius = radius;
}
void move_x(double x){
    this.x = x;
}
void move_x(double x , double y){
    this.x = x;
    this.y = y;
}
void move_y(double y){
    this.y = y;
}
void move_y(double x,double y){
    this.x = x;
    this.y = y;
}
void scale(double a){
    radius *= a;
}}

`

class CircleOverloadMethod{
public static void main(String args[]){
    Circle c = new Circle(4);
    Circle d = new Circle(4);
    System.out.println("x軸上で移動");
    c.move_x(2);
    c.scale(0.5);
    System.out.println("c.x      = " + c.x);
    System.out.println("c.y      = " + c.y);
    System.out.println("c.radius = " + c.radius);
    c.move_x(-2,-2);
    c.scale(2);
    System.out.println("c.x      =" + c.x);
    System.out.println("c.y      =" + c.y);
    System.out.println("c.radius =" + c.radius);
    System.out.println("y軸上で移動");
    d.move_y(2);
    d.scale(0.5);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);
    d.move_y(-4,-4);
    d.scale(4);
    System.out.println("d.x      =" + d.x);
    System.out.println("d.y      =" + d.y);
    System.out.println("d.radius =" + d.radius);

}}

です。このコードについても理解しきれていない部分やメソッドをオーバーロードするという意味やメソッドとシグネチャが同じになりという意味もよくわかっていません。
記述通りエラーをだしたいのと、y軸上に移動するようなmove()メソッドの作成の仕方もあるのでしたら教えていただきたいです。よろしくお願いします。