Reputation: 8541
I'm a Java newbie and ran into a bit of a problem. I want a class to become another class. It's hard to explain it the abstract way, so I'll give you an example.
public class WorldGuard extends WorldGuardPlugin {
public WorldGuard(Plugin parent) {
WorldGuardPlugin plugin = parent.getServer().getPluginManager().getPlugin("WorldGuard")
// make plugin the actual class
}
}
WorldGuard
should act like some kind of a wrapper here. When constructed it gets one parameter parent
on which base it finds an instance of WorldGuardPlugin
. The class should now become that instance.
It's simple in JavaScript, I just return the instance, but I can't do this in Java.
Upvotes: 0
Views: 91
Reputation: 4264
You should call the copy constructor of WorldGuardPlugin
. What I mean is that WorldGuardPlugin
should have a constructor that can create a copy of a given instance of the class like:
WorldGuardPlugin pg = new WorldGuardPlugin(anInstance);
If this is the case then you are in luck. You can simply do:
public class WorldGuard extends WorldGuardPlugin {
public WorldGuard(Plugin parent) {
super( parent.getServer().getPluginManager().getPlugin("WorldGuard"));
}
}
This will make "WorldGuard act like some kind of a wrapper here". You can still call the methods defined in WorldGuardPlugin
on an instance of WorldGuard
while being able to add methods to WorldGuard
itself.
Upvotes: 1
Reputation: 115328
Class cannot become other class.
I think you can choose among the following possibilities.
WordGuard
class will wrap actual instance of other class and delegate all calls there. Upvotes: 1
Reputation: 57192
You can't change the type of object being constructed in a constructor. A constructor, by definition, constructs that class (e.g. A
's constructor creates a type A
).
In your example, WorldGuard
extends WorldGuardPlugin
, which means that WorldGuard
is a type of WorldGuardPlugin
. Maybe there is a way to initialize the WorldGuardPluin
class (using a call to super
in the constructor) with the properties that you want.
Upvotes: 0
Reputation: 27474
I'm not quite sure what you're trying to do. A class cannot "become another class". But perhaps your problem is just that you're trying to use a constructor when you should be using a plain function. Maybe what you want to do is this:
public class WorldGuard extends WorldGuardPlugin
{
public static WorldGuard getFromPlugin(Plugin parent)
{
return (WorldGuard) parent.getServer().getPluginManager().getPlugin("WorldGuard");
}
}
That would get the object via parent and return it as a WorldGuard object.
Upvotes: 2