Not exactly sure how to phrase this, sorry for ambiguous title.
Anyways, here’s basically my cuestión.
Basically, I know how I can pass values into the class using the contructor and functions and such, like,
class bob {
int value;
public bob(int x) {
value = x;
}
}
bob test = bob(5);
But how do you handle things like operators and such? Like, if a person added the classes together:
bob test1 = bob(5), test2 = bob(3), test3 = test1 + test2;
How could I make it actually do something is a person tried to add the two instantiated objects together?
Or if I said something like,
bob test = 5;
How could I do something with a value you initialize it to have?
You can’t do either in Java. Java’s operators only work for primitive types (and String as a special exception), and regular objects can only be initialized with compatible objects or
null.Instead, you should define appropriate methods and constructors:
then use
Since you can’t do
bob test = 5;, just dobob test = new bob(5);.