電子書籍の厳選無料作品が豊富!

java

三角形のオブジェクトを作ってる最中なのですが
エラーが多くて動きません。
自分が思うには他のクラス(Circle.java,Line.java,Point.java)と連携してないのかなと思ってます。

以下ソースです。


public class Sample70 {

public static void main(String[] args) {
Point o;//点オブジェクト
Line line;//線lineオブジェクト
Triangle triangle;//三角形triangleオブジェクト

Circle circle;//円circleオブジェクト
o = new Point();
line = new Line();
triangle = new Triangle();
circle = new Circle();
o.x = 0;
o.y = 0;
Point point = new Point();
point.x = 2;
point.y = 3;
line.p0 = point;
point = new Point();
point.x = 4;
point.y = 9;
line.p1 = point;
triangle.p0 = new Point();
triangle.p0.x = 3;
triangle.p0.y = 9;
triangle.p1 = new Point();
triangle.p1.x = 4;
triangle.p1.y = 3;
triangle.p2 = new Point();
triangle.p2.x = 4;
triangle.p2.y = 6;
circle.p0 = new Point();
circle.p0.x = 10;
circle.p0.y = 10;
circle.r = 1;
System.out.println(o.x);
System.out.println(line.p0.y);
System.out.println(line.p1.x);
System.out.println(triangle.p2.y);
System.out.println(circle.r);

}

}

エラーメッセージは
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
x cannot be resolved or is not a field
y cannot be resolved or is not a field
x cannot be resolved or is not a field
y cannot be resolved or is not a field
p0 cannot be resolved or is not a field
x cannot be resolved or is not a field
y cannot be resolved or is not a field
p1 cannot be resolved or is not a field
p0 cannot be resolved or is not a field
p0 cannot be resolved or is not a field
p0 cannot be resolved or is not a field
p1 cannot be resolved or is not a field
p1 cannot be resolved or is not a field
p1 cannot be resolved or is not a field
p2 cannot be resolved or is not a field
p2 cannot be resolved or is not a field
p2 cannot be resolved or is not a field
p0 cannot be resolved or is not a field
p0 cannot be resolved or is not a field
p0 cannot be resolved or is not a field
r cannot be resolved or is not a field
x cannot be resolved or is not a field
p0 cannot be resolved or is not a field
p1 cannot be resolved or is not a field
p2 cannot be resolved or is not a field
r cannot be resolved or is not a field

at Sample70.main(Sample70.java:14)

多分フィールドがないよという意味なのかも知れませんが
中々実行がうまくいかず困ってます。
駄目出しで良いのでご教授頂ければと思います。
http://www.atmarkit.co.jp/ait/articles/0503/19/n …

「三角形オブジェクト」の質問画像

A 回答 (3件)

> x cannot be resolved or is not a field



おそらくは
クラス Point のフィールド x のアクセス修飾子が省略されており、
クラス Point と Sample70 が別のパッケージ(ディレクトリ)なので、
フィールドの制限である「同じパッケージ内のみ」に引っかかり、
Sample70 から x が見つからずにコンパイルエラーとなっています。

修正するならば、
1. フィールド x に public の修飾子を付ける (おすすめ)
2. クラス Point と Sample70 を同じパッケージにする

参考) Java のアクセス修飾子
private : 同じクラスのみ
protected : 同じパッケージ内 または サブクラス
public : すべて
省略時 : 同じパッケージ内のみ

参考) Java 言語仕様 / セキュリティの概要
https://docs.oracle.com/javase/jp/8/docs/technot …
    • good
    • 0
この回答へのお礼

解決に結びつきました!ありがとうございます!助かりました!

お礼日時:2016/09/28 14:27

肝心のCircle, Line, Triangleのソースは?

    • good
    • 0

どうもインスタンス化のタイミングでエラーになっていませんから、そういうフィールドがないんじゃないんですかね。

    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!