We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Having a hard time figuring out when and why this should be done. And if I don't need to do it, how do I update older code to not use it?
How would this class be rewritten to avoid using PVector inheritance?
class Bullet extends PVector {
PVector vel;
Bullet(PVector loc, PVector vel) {
super(loc.x, loc.y);
this.vel = vel.get();
}
void update() {
add(vel);
}
void display() {
fill(0, 0, 255);
ellipse(x, y, 3, 3);
}
}
Answers
Is this homework?
Technically, you think of a bullet to be a vector that contains another vector. Based on the constructor you provided, you can see that the first field is location which is assigned to itself using the super() function. So, base on this information and the current layout of your code, this could be rewritten as:
Kf
Thanks for that. Not for homework, but we have never talked about extending PVector in class and I see it in examples here quite often.
If I'm not mistaken, your code could be written like:
but this doesn't work in the example on that page, so I'm wondering what sorcery I'm missing.
Kf
THANK YOU! So back to my original question, is there a preference as to whch way to do this? Am I just being taught a more newb-friendly way?
That was merely a "hack" for typing in
loc.add(vel);
as justadd(vel);
andloc.x
as simplyx
. :ar!The only problem w/ such approach is for when exporting it to the web via Pjs. :|
B/c we can't use
extends
for Processing's own classes in Pjs, but ours only. :-@Here's an alternative version using point() + strokeWeight() + pushStyle() btW: :-bd
Is there a preference? I think if it works, it is good. At the end, it is part of your design. If you have an object that its intrinsic and core functionality are solely described by a PVector class, then there is nothing wrong with extending this class. When it comes to design, I make an effort of seeing the big picture. Do I have to do that? Should it be like that? Can it be done differently? What trade offs do I get and are they meaningful? Can this approach make maintenance easier?
For small projects, either approach is more based on taste in my opinion.
Kf
Thanks both, really appreciate this!
Yes there is a preference, actually not just a preference but a correct way to do it. :)
1) Inheritance.
Inheritance represents a kind of relationship between classes so ...
suggests that
Bullet
is a kind ofPVector
--- WRONG!2) Aggregation / Composition
When describing a Bullet object we need to consider what information we need to know about it e.g. position, velocity, acceleration, weight ... These describe some aspect of a
Bullet
so we say that aBullet
is an aggregation of position, velocity etc.suggests that Bullet is composed of position, velocity etc. --- CORRECT
NOTE: pos, velocity ... are called fields or attributes (I prefer attributes because it is more accurately describes what they represent)
It might seem a small thing because it is possible to write software using inheritance or aggregation for the relationship between
Bullet
andPVector
, but if you pick the wrong one (i.e. inheritance) you will end up writing crap code to overcome the invalid relationship.Thanks quark... good way to think about it.