Reputation: 9824
I am trying to pass a Class
to a method. The Class
changes as the program runs, so I'd like to reuse the same method through out my program instead of calling the same functions throughout my resetWords()
method.
Here is where I am stuck:
private void getWords(Class c) {
singles = c.getSingleSyllables();
doubles = c.getDoubleSyllables();
triples = c.getTripleSyllables();
quadruples = c.getQuadrupleSyllables();
quintuples = c.getQuintupleSyllables();
}
private void resetWords() {
if (generated.equals("SOMETHING")) {
Something c = new Something();
getWords(c);
}
else if (generated.equals("ANOTHER")) {
Another c = new Another();
getWords(c);
}
}
Upvotes: 2
Views: 292
Reputation: 1
Your question does not provide enough detail to answer clearly.
Depending upon your design / end-goals, there are three concepts you should take a look at and understand:
An Interface will define the methods that classes that implement the interface must provide. Each class that implements the interface must provide the code for the method.
An Abstract Class will provide a single implementation of the behavior that you are looking for.
Reflection is an advanced concept. I would recommend you stay away from it at this time - but you should be aware of it.
Given your example code, you may want to use an Abstract Class. Designed properly, you can increase flexibility/reuse by defining an interface, implementing that interface with an Abstract Class and then extending that Abstract Class as needed. Every class that extends the Abstract will pick up the default implementation you provided in the Abstract class definition; the Interface will make it easy for you to extend in the future.
Upvotes: 0
Reputation: 8361
I think what you are looking for is an interface. You should declare an interface like this:
public interface Passable
{
public List<String> getSingleSyllables();
public List<String> getDoubleSyllables();
// ...
}
Then, let Something
and Another
implement them:
public class Something implements Passable
{
// method declarations
}
Now, change your method to this:
private void getWords (Passable c) {
singles = c.getSingleSyllables();
doubles = c.getDoubleSyllables();
triples = c.getTripleSyllables();
quadruples = c.getQuadrupleSyllables();
quintuples = c.getQuintupleSyllables();
}
Upvotes: 3
Reputation: 1277
If the classes always implement getSingleSyllables()
, getDoubleSyllables()
, etc.. then you should consider inheriting from an abstract class, or implementing an interface.
Then...
private void getWords(YourInterface / YourAbstractClass foo) {
...
}
Upvotes: 1
Reputation: 691635
You're confusing classes, and objects.
What you're passing to getWords()
is an object. In the first case, it's an object of type Something
. In the second case, it's an object of type Another
.
The only way for such code to work is to define a common base class or interface for Something
and Another
(let's call it HavingSyllabes
), containing the 5 methods used in getWords()
: getSingleSyllables()
, getDoubleSyllabes()
, etc. And the signature of getWords()
should be
private void getWords(HavingSyllabes c)
Upvotes: 1
Reputation: 40734
A little vague what you're asking but perhaps create an Interface
that defines all of the getXSyllables()
methods. Have your classes (Something
and Another
) implement that Interface
. Finally, define getWords as private void getWords(YourInterface c)
.
Upvotes: 2