java - Accessing superclass variables from an enum -
is there way set variable held in enums parent/superclass within enum itself? (the following doesn't compile, illustrates i'm attempting achieve)....
class myclass{ objecttype type; string somevalue; public void settype(objecttype thistype){ type = thistype; } enum objecttype { ball{ @override public void setvalue(){ somevalue = "this ball"; //some value isn't accessible here } }, bat{ @override public void setvalue(){ somevalue = "this bat"; //some value isn't accessible here } }, net{ @override public void setvalue(){ somevalue = "this net"; //some value isn't accessible here } }; public abstract void setvalue(); } }
then, so:
myclass myobject = new myclass(); myobject.settype(objecttype.ball);
after doing above, 'somevalue' string of myobject should set 'this ball'.
is there way this?
you following, if want myclass.somevalue equal somevalue of enum, somevalue can retrieved enum i'd not bother having somevalue on myclass @ all, , retrieve enum when required
public class myclass { objecttype type; string somevalue; public void settype(objecttype thistype) { this.type = thistype; this.somevalue = thistype.getsomevalue(); } enum objecttype { ball ("this ball"), bat ("this bat"), net ("this net"); private final string somevalue; objecttype(string somevalue) { this.somevalue = somevalue; } public string getsomevalue() { return somevalue; } } }
Comments
Post a Comment