简体   繁体   中英

Adventure game constructor method problems Java

I'm creating a text adventure game in Java. I've created an exit, room, creature and an item class, and now I'm instantiating and putting values for them in my main class.

I'm getting this problem though: as I'm creating exits for my room, I'm getting a compiler error saying

Constructor Exit in class Exit cannot be applied to given types

This is the line of code I'm getting the compiler error for:

Exit exit1 = new Exit("Exit1", "Exit description", "Exit transition text");

and in my Exit class I have this constructor method:

public void Exit(String exit, String exitDescription, String exitTransition) {
    this.Exit(exit, exitDescription, exitTransition);       
}

but every time I tell the IDE (Netbeans) to correct the compiler error, it generates this in my Exit class:

Exit(String exit1, String exit_description, String exit_transition_text) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

So my question is, why doesn't my constructor method work? Also, I had help from my professor and I believe he said that I don't want the generated code. Don't remember exactly though.

This is not a constructor:

public void Exit(String exit, String exitDescription, String exitTransition) {

constructors have no return type.

This is a constructor:

public Exit(String exit, String exitDescription, String exitTransition) {

Of course you know that even if this were a valid constructor the code in its body doesn't make much sense as it appears you're trying to have the constructor call itself recursively:

// void removed
public Exit(String exit, String exitDescription, String exitTransition) {
    this.Exit(exit, exitDescription, exitTransition); // this calls itself!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM