Java noob をここで完成させてください。現在独学中。最近、クラスを使用して別のクラスから情報を取得し、それを出力する基本的なプログラムを作成しようとしました。このエラーが発生し続けます:
error cannot find symbol
System.out.print1n(ljames.weight);
symbol: variable weight
location: class ljames
これが私のコードです:
私を助けてください。
Java noob をここで完成させてください。現在独学中。最近、クラスを使用して別のクラスから情報を取得し、それを出力する基本的なプログラムを作成しようとしました。このエラーが発生し続けます:
error cannot find symbol
System.out.print1n(ljames.weight);
symbol: variable weight
location: class ljames
これが私のコードです:
私を助けてください。
変数の可視性だけだと思います。これらの変数を public として宣言していないため、他のクラスからアクセスできません。
関数をパブリックとして宣言しました。他の場所からアクセスできます。
パブリック コンストラクターを作成し、変数の get-accessor を作成することをお勧めします。それらをコンストラクターで設定し、get-accessor で読み取ります。このようにして、データを安全に保ち、必要なときに変更することができます。
class Data {
String height;
int weight;
int depth;
public Data(String height, int weight, int depth) {
this.height = height;
this.weight = weight;
this.depth = depth;
}
public string getHeight() {
return height;
}
public int getWeight() {
return weight;
}
public int getDepth() {
return depth;
}
}
クラスljamesには、Dataクラスにあるweightという名前のフィールドがありません。