Hit a brick wall with OOP
I want to be able to "pass" an object, to another object. I'm busy objectifying my to-be "game" (see other thread -> "A bit of Advice"). Anyway, I'll use pseudo code to help you understand what I want to do. Hopefully you can help me out!
mainClass:
public class mainClass {
public static void main(String args[]) {
classOne objOne = new classOne(5);
//create this object and "pass" the one I created to it.
classTwo objTwo = new classTwo(objOne);
//double a in this class
objOne.doubleA();
//and then triple it, after retrieving it using the "passed" object
System.out.prinln(objTwo.tripleTheDouble());
//should in theory print 30
}
}
classOne:
public class classOne {
public int a;
//some more properties
public classOne(int a) {
this.a = a;
}
public int doubleA() {
this.a = this.a * 2;
return this.a;
}
//some more methods
}
classTwo:
public class classTwo {
classOne one;
int newA;
public classTwo(classOne obj) {
this.one = obj;
}
public int tripleTheDouble() {
newA = one.a * 3;
return newA;
}
}
I have NOOOO idea if that code would even work. I just used it as an exmaple of what I want to do. I want to do this sort of thing to pass player objects to board objects, and board objects to game objects etc. etc.
How would I do that?
Thanks in advance!
EDIT: I have seen APIs that require you to pass certain objects into class methods (for instance creating a canvas object, and then passing that object to a draw method or something), so I know it can be done, I'm just not sure how.






Cartoon Clouds
Mountains
Sunrise
Clouds
Green Clouds
None















Help