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

2019/4/30

Very basic question

CodeTime CodeTime

2019/4/30

#
So I have some questions about code and how I could change it. (this came from the tutorial.
if (worm != null) {
   World world;
   world = getWorld();
   world.removeObject(worm);
 }
How would I change it if my world class is named "earth" for example?
danpost danpost

2019/4/30

#
CodeTime wrote...
So I have some questions about code and how I could change it. (this came from the tutorial. << Code Omitted >> How would I change it if my world class is named "earth" for example?
It does not matter what your World subclass name is. All instances of subclasses of World are still World objects. So, the code need not be changed if you change the name of the World subclass. Now, although not necessary, you could be more specific as to what type world it actually is by changing line 2 to the following:
earth world;
This declares a local reference variable to an object of type earth, rather than of type World (keep in mind, however, it is still a World object). This change would only be necessary if you had to access a member (field or method) of (contained in) the earth class itself. Since the above code only accesses the removeObject method (which is a member of the World class), that change is unnecessary.
You need to login to post a reply.