Can we over-load "+" in Java?

S

Shawn

Hi,

Suppose I have following classes:

class Person {
....

}

class Team {
//how to have a member variable which holds an array of objects of Person?

....
}

Person person1= new Person("John Smith");
Person person2= new Person("Linda King");
Person person3= new Person("Ed Johnson");

//here is what I want:
Team BestTeam;
BestTeam = person1 + person2;
//now an object of class Team was created and this team has the two people

BestTeam = BestTeam + person3;
//now Ed Johnson joined the team

BestTeam = BestTeam - person3;
//now Ed Johnson left the team

Can we overload "+" and "-" to achieve the effect above? If not(which is
my feeling), how can we achieve something similar? If we use a method
(say,join(), left()), please give me full implementation of the method?
 
P

Patricia Shanahan

Shawn said:
Hi,

Suppose I have following classes:

class Person {
...

}

class Team {
//how to have a member variable which holds an array of objects of
Person?

...
}

Person person1= new Person("John Smith");
Person person2= new Person("Linda King");
Person person3= new Person("Ed Johnson");

//here is what I want:
Team BestTeam;
BestTeam = person1 + person2;
//now an object of class Team was created and this team has the two people

BestTeam = BestTeam + person3;
//now Ed Johnson joined the team

BestTeam = BestTeam - person3;
//now Ed Johnson left the team

Can we overload "+" and "-" to achieve the effect above? If not(which is
my feeling), how can we achieve something similar? If we use a method
(say,join(), left()), please give me full implementation of the method?

Java does not allow user-defined overloading of operators. Even if it
did, I would argue against using + for anything other than addition of
numbers, - for anything other than subtraction of numbers etc. I've seen
C++ code that uses operators for arbitrary functions, and find it quite
unreadable.

For this sort of task in Java, I would design a class Team with methods
such as "add", "remove", "isMember", and possibly a method returning an
iterator over the members.

Team bestTeam = new Team();
bestTeam.add(person1);
bestTeam.add(person2);
bestTeam.add(person3);
bestTeam.remove(person3);

etc.

Inside bestTeam, I would use e.g. a java.util.Set, with implementing
class chosen based on the sort of operations, to remember the current
members.

Patricia
 
S

Shawn

Patricia said:
Java does not allow user-defined overloading of operators. Even if it
did, I would argue against using + for anything other than addition of
numbers, - for anything other than subtraction of numbers etc. I've seen
C++ code that uses operators for arbitrary functions, and find it quite
unreadable.

For this sort of task in Java, I would design a class Team with methods
such as "add", "remove", "isMember", and possibly a method returning an
iterator over the members.

Team bestTeam = new Team();
bestTeam.add(person1);
bestTeam.add(person2);
bestTeam.add(person3);
bestTeam.remove(person3);

etc.

Inside bestTeam, I would use e.g. a java.util.Set, with implementing
class chosen based on the sort of operations, to remember the current
members.

Patricia

Thank you very much. You have solved the doubts in my mind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,077
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top