私はこれを理解しようとするのに9時間を費やしましたが、これはうまくいきません。 main()メソッドでののインスタンスを作成すると、「シンボルが見つかりません」というエラーが返されます。コンストラクタに配列を渡す?
public class ship {
private String thisArray [] = {"I", "hate", "you"};
// my constructor:
public ship (String [] thisArray) { this.thisArray = thisArray };
public String toString() { String out; return out = thisArray; }
}
APPLICATION:事前に助けを
class shipdemo
{
public static void main (String [] args) {
ship = new ship(thisArray[2]);
ship.toString();
// I am expecting this to print: "YOU" but instead it gives me:
// error: cannot find symbol
}
}
おかげで、。 ship
とthisArray
として
コンストラクタは配列を必要とします。 'thisArray [2]'は配列の一つの要素、すなわち 'String'です。あなたが 'String [] []'として宣言された別の 'thisArray'を持っていない限り。しかし、それは "シンボルが見つかりません"というエラーにつながりません。それを診断するには、プログラム全体を見る必要があります。 – ajb
'thisArray [2]'は何と思いますか? – Li357
また、 'thisArray'は' ship'クラスにあり、 'main'メソッドを含む' shipdemo'クラスにはありません。 'main'メソッドは変数' thisArray'を見ることができません。別のクラスにあるからです。 – Jesper