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

2020/9/9

Newton's Lab: this(new Vector()) what does this mean??

disassociated disassociated

2020/9/9

#
In the scenario Newton's Lab in the Greenfoot Book this statement in the smoothMover class I don't understand can anybody help?? public SmoothMover() { this(new Vector()); }
danpost danpost

2020/9/9

#
The SmoothMover class used for Newton's Lab has two constructors -- one without any parameters and one with a Vector parameter. The one you show (with no parameters) redirects execution to the other one, supplying a vector (albeit, a { 0, 0 } one). This redirect ensures that a Vector object is created for the velocity. As a side note -- this acts like a function call, in that you can have code following your line 3 that is only executed when this particular constructor is called. However, you can NEVER have any executable code within that constructor before that line. Constructor redirects, whether using super or this, must always be placed first, before any other codes in the constructor.
disassociated disassociated

2020/9/9

#
Thank you, that makes sense but didn't we already create the vector in space class as: addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y);
danpost danpost

2020/9/9

#
disassociated wrote...
didn't we already create the vector in space class as: addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y);
The Body constructor has that vector being added to the vector in question. An error would ensue had it not already been created.
disassociated disassociated

2020/9/10

#
danpost wrote...
disassociated wrote...
didn't we already create the vector in space class as: addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y);
The Body constructor has that vector being added to the vector in question. An error would ensue had it not already been created.
Ok I din't get it :) Can you open that up or give a hint about the concept where I can read more? The run sequence is like this right? Space/ Constructor Vector / Constructor SmoothMover / Constructor Body / Constructor
danpost danpost

2020/9/10

#
disassociated wrote...
Can you open that up or give a hint about the concept where I can read more? The run sequence is like this right? Space/ Constructor Vector / Constructor SmoothMover / Constructor Body / Constructor
Let me just point out a thing here. When an Object instance is created, it is created from the bottom up. All extending classes are built on their supers. So, when you create a body, the SmoothMover portion is created before the Body portion. Think of it as such that the Body object inherits pre-defined traits provided by the SmoothMover class (which, in turn inherits from Actor -- from java.lang.Object). So, just by calling "new Body(...)", all non-parameterized constructors of all classes it inherits from are executed first. Therefore, the order is (1) Object; (2) Actor; (3) SmoothMover; then (4) Body. The direction Vector is created during the SmoothMover build; so when Body builds, there will be a vector to add the parameter vector to.
You need to login to post a reply.