1

Java noob をここで完成させてください。現在独学中。最近、クラスを使用して別のクラスから情報を取得し、それを出力する基本的なプログラムを作成しようとしました。このエラーが発生し続けます:

error cannot find symbol

System.out.print1n(ljames.weight);

symbol: variable weight
location: class ljames

これが私のコードです:

http://shrib.com/sEyGhFZr

私を助けてください。

4

2 に答える 2

0

変数の可視性だけだと思います。これらの変数を 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;
    }
}
于 2013-06-23T00:09:45.837 に答える
0

クラスljamesには、Dataクラスにあるweightという名前のフィールドがありません。

于 2013-06-23T00:13:33.150 に答える