Jump to content

Recommended Posts

Posted

I'm not sure whether this should be in forum for just java in general or modding with forge, but since this is for a mod I'm making, I'll put it here.

 

I'm trying to make my Item take a class name in it's constructor. so

public ItemVehicle(int par1, String entv, String icon)
    {
        super(par1);
        this.maxStackSize = 1;
        this.setCreativeTab(CodeLyoko.LyokoTabs);
        entityVehicle = entv;
        iconString = icon;
    }
String entityVehicle = "";

and then later in the class I want to have something like this

EntityVehicle var35 = new entityVehicle(/**constructor information**/)

I want to use the string as a class name, so entityVehicle would end up equalling something like "EntityOverboard". Is there a syntax error?  Or something else?

Posted

Not too sure what you're asking as your question doesn't seem to be worded greatly.

 

From my understanding (just going to include this to make sure we're both talking about the same thing) polymorphism is, to put it simply, extending or implementing a class or interface.

 

going on this I'm guessing that you want to change the type of vehicle based on the string you're passing it. (if this assumption is wrong please tell me, i couldn't really get an excellent grasp for what you were asking)

 

in that case you could do something like:

EntityVehicle var35;
    if(entityVehicle.equals(VehicleType1.class.getSimpleName())) // VehicleType1 extends EntityVehicle
        var35 = new VehicleType1(/*constructor stuff*/);
    else if(entityVehicle.equals(VehicleType2.class.getSimpleName())) // VehicleType2 extends EntityVehicle
        var35 = new VehicleType2(/*constructor stuff*/);
    else
        var35 = new EntityVehicle(/*constructor stuff*/);

 

i believe you could do something like what you're asking using the Java reflection api, which if i recall correctly allows you to get a class and a constructor form a string, but i'm not very familiar with reflection so can't help that much with that but i can say there are probably many good tutorials on google.

 

One problem you could have with that however is you may not know the required constructors, however if you know that all the constructors take all the same parameters then this shouldn't be a problem.

 

Also i think that reflection can be slower in some cases than direct accesses to the class so use wisely.

 

hope that helps, i could be answering a different question, in which case sorry for wasting your time and *bump*

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.