Enums with attributes Java -
each color has own static attribute - number. want able change value method. can using enums somehow? or perhaps differently:
public enum color { red, orange, yellow; } color.red.setvalue(x); color.red.getvalue();
or have color class?
public red extends color { private static int x; public int getredvalue(){ return x; } public void setredvalue(int x){ this.x = x; } }
yes, can following:
enum colour{ red(1), blue(2); public int value; colour(int valuearg){ value = valuearg; } /*public setvalue(int a){ value = a; } public getvalue(){ return value; }*/ } public class test{ static colour colour = colour.blue; public static void main(string[] args){ colour.value = 3; //colour.setvalue(3); } }
you can variable type you'd like. here, each instantiation of colour
enum has associated integer value. optionally, make value
field private , create accessor , mutator methods (see code comments). how works provide value field via constructor call when make new instantiation of enum. can add more fields , arguments constructor wish.
Comments
Post a Comment