-1
誰でも、a2オブジェクトを作成した後にコンストラクタが値を変更しなかったことを教えてください。なぜならこのライン a1.clear();
あなたの明確な方法は、あなたの静的xのorginalの値を変更している、とy変数のa2オブジェクトを作成した後、コンストラクタが値を変更しなかった理由を教えてください。
public class HelloWorld
{
static int x; // static datamembers
static int y; // static datamembers
HelloWorld() //constructor
{
x = 9999;
y = 9999;
}
static void display() // static method
{
System.out.println("The value of x is:"+x);
System.out.println("The value of y is:"+y);
}
void clear()
{
this.x = 0; // this pointer
this.y = 0; // this pointer
}
public static void main(String []args)
{
HelloWorld a1 = new HelloWorld(); // instance of class
HelloWorld a2 = new HelloWorld(); // instance of class
a1.display(); // a1 object calls the display
a1.clear(); // this pointer clears the data
a1.display(); // cleared data is displayed
a2.display(); // a2 object calls the display but the values are 0 and 0 why not 9999 and 9999, why didn't the constructor get called?
}
}
あなたのプログラムを実行しましたか?値は0と0に変更されました。あなたの質問は本体に関係していませんか? Javaの静的について理解する必要がありますhttps://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html –
これはどのようなプログラミング言語ですか?ご使用の言語で質問にタグを付けてください。あなたの質問を更新するには、投稿の下の** "[編集]" **リンクをクリックしてください。ありがとうございました。 – Pang