This site requires JavaScript, please enable it in your browser!
Greenfoot back
Logic_Greenfoot
Logic_Greenfoot wrote ...

2022/11/20

How can I modify the data of another class in one class? How to write this code.

Logic_Greenfoot Logic_Greenfoot

2022/11/20

#
I'm a rookie. I don't know how to modify the data of another subclass in a subclass of a class. (I write in class a: press the left key, modify a data in class b)
danpost danpost

2022/11/20

#
Logic_Greenfoot wrote...
I'm a rookie. I don't know how to modify the data of another subclass in a subclass of a class. (I write in class a: press the left key, modify a data in class b)
Prefix the object with its subclass name in parentheses. For example:
((AnotherClass)referencedObject).variableName++;
The inner part is casting the referenced object as its specific type, so the variable in that class can be seen.
Logic_Greenfoot Logic_Greenfoot

2022/11/20

#
danpost wrote...
Logic_Greenfoot wrote...
I'm a rookie. I don't know how to modify the data of another subclass in a subclass of a class. (I write in class a: press the left key, modify a data in class b)
Prefix the object with its subclass name in parentheses. For example:
((AnotherClass)referencedObject).variableName++;
The inner part is casting the referenced object as its specific type, so the variable in that class can be seen.
Thank you very much. In order to run this code, do I need to reference the Java library?
danpost danpost

2022/11/21

#
Logic_Greenfoot wrote...
In order to run this code, do I need to reference the Java library?
No.
You need to login to post a reply.