i have class has several enums representing names of different types of devices.
public class devicenames { public enum microsoft { devicename1, devicename2, devicename3 } public enum apple { devicename1, devicename2, devicename3 } }
i have interfaces different concrete devices implement (the interface specifies ways can communicate)
public interface ipdevice{ //factory method device createnewipdevice(devicenames.enum<e> devicename); } public interface usbdevice{ //factory method device createnewusbdevice(devicenames.enum<e> devicename); }
and have abstract class
public abstract class device { string devicename; protected device(string devicename) { this.devicename = devicename; } }
and concrete implementations of abstract class implement 1 or more of interfaces
public class microsoftdevice implements ipdevice{ @override public device createnewipdevice(devicenames.microsoft devicename) { return super(devicename.tostring()); } } public class appledevice implements ipdevice, usbdevice { @override public device createnewipdevice(devicenames.apple devicename) { return super(devicename.tostring()); } @override public device createnewusbdevice(devicenames.apple devicename) { return super(devicename.tostring(); } }
i want use enum ensure give valid device names (instead of them passing in string)
how can have interface method take parameter can take generic enum type enum in devicenames class?
i asked follow question here. accepted answer solved question.
to quote @andreas (the 1 answered other question)
you make interface take generic parameter
the working code looks (the code not included here stays same):
public interface ipdevice<devicename extends enum> { //factory method device createnewipdevice(devicename devicename); } public interface usbdevice<devicename extends enum> { //factory method device createnewusbdevice(devicename devicename); } public class microsoftdevice implements ipdevice<devicenames.microsoft> { @override public device createnewipdevice(devicenames.microsoft devicename) { return super(devicename.tostring()); } } public class appledevice implements ipdevice<devicenames.apple>, usbdevice<devicenames.apple> { @override public device createnewipdevice(devicenames.apple devicename) { return super(devicename.tostring()); } @override public device createnewusbdevice(devicenames.apple devicename) { return super(devicename.tostring(); } }
Comments
Post a Comment