简体   繁体   中英

Passing a enum into a constructor in Java

I am learning java. I would like to have a enum as a parameter in my constructor. But I am getting an error (I have my enum in a separate class that is public and named AvailabilityState {AVAILABLE,ORDERED,REMOVED }

public class Facultymember extends User {
private  int MAX_GENERALBOOKS = 5;
private  int MAX_AUDIOBOOKS = 2;
private AvailabilityState aStatus;

public Facultymember(int genbook, int audbook,AvailabilityState aStatus ){
        this.MAX_GENERALBOOKS=genbook;
        this.MAX_AUDIOBOOKS=audbook;
                this.aStatus  = aStatus;
    }



@Override
    public String toString() {
        return "Facultymember {" + "MAX_GENERALBOOKS=" + MAX_GENERALBOOKS+ ",  MAX_AUDIOBOOKS =" +  MAX_AUDIOBOOKS  + "AvailabilityState," + aStatus +  '}';
    }

}**

If you require a parameter of type AvailabilityState, you should provide it, like so:

User availableFaculty = new Facultymember(5,2, AvailabilityState.AVAILABLE);
User orderedFaculty = new Facultymember(5,2, AvailabilityState.ORDERED);
User removedFaculty = new Facultymember(5,2, AvailabilityState.REMOVED);

Alternatively, define another constructor with default availability state:

public Facultymember(int genbook, int audbook) {
    // assuming availability by default
    this(genbook, audbook, AvailabilityState.AVAILABLE);
}

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