前提・実現したいこと
継承外の値を取得(?) (複数の)座標の値を表示する方法(?) (問題点を自分自身で上手く言語化できていないため、下記を読んで下さい。)
発生している問題・エラーメッセージ
・エラー 下記のソースコードから "[円を描画] 中心点(Point@39d3030a)"と表示される ・目標 "[円を描画] 中心点(100, 100)"と表示させたい。
該当のソースコード
Java
1public class Main{ 2 public static void main(String[] args){ 3 Figure figure = new Circle(100, 100, 20); 4 figure.draw(); 5 } 6}
public interface Figure{ void draw(); }
public class Point extends Object { private int x; private int y; public Point() {this.x = 0; this.y = 0;} public Point(int x, int y){this.x = x; this.y = y;} public int getX() {return this.x;} public void setX(int x) {this.x = x;} public int getY() {return this.y;} public void setY(int y) {this.y = y;} }
public abstract class Shape implements Figure { public abstract void draw(); }
public class Circle extends Shape{ private Point center; private int radius; public Circle(int x, int y, int radius){ this.center = new Point(x, y); this.radius = radius; } public void draw(){ System.out.println("[円を描画] 中心点(" + this.center); } }
試したこと
Circle.draw()における、this.centerが違うということには見当がついています。
Point.getX()やPoint.getY()かな?、とも思いましたが、staticではない(=Class Fieldではない)ため、使用できませんでした。
補足質問
Figure figure = new Circle(100, 100, 20); ではなく Circle circle = new Circle(100, 100, 20); の場合だと、エラーが発生したのですが、この理由もわからないため教えてください。 よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/24 06:51
2018/03/24 06:53
2018/03/24 07:01
2018/03/24 07:39
2018/03/24 07:44
2018/03/24 09:40