class - Is it possible to use Enum constants as templates for instantiable classes in Java? -


sorry if question bit unclear; i'm finding little tough find wording. spent several hours screwing around in eclipse, cruising javadoc, , google, so. learned lot, didn't find answer.

what i'd able define enum, eg.:

public enum animals { cow, chicken, sheep, horse; } 

and have each enum constant define instantiable class that's not local class. following work? , if not, why, , would?

in file:

abstract public class animal { private string namestring; public string getname() { return namestring; } } 

in another:

public enum animals { cow ((new animal() { private boolean hasmilk; { namestring = "cow"; hasmilk = false; } public boolean hasmilk() { return hasmilk; } }).getclass()), chicken ((new animal() { private boolean hasfeathers; { namestring = "chicken"; hasfeathers = true; } public boolean hasfeathers() { return hasfeathers; } }).getclass()), sheep ((new animal() { private boolean isshorn; { namestring = "cow"; isshorn = false; } public boolean isshorn() { return isshorn; } public void shear() { isshorn = true; } }).getclass()), horse ((new animal() { { namestring = "cow"; } }).getclass()); private class<? extends animal> animalclass; private animals(class<? extends animal> a) { animalclass = a; } public class<? extends animal> getanimalclass() { return animalclass; } } 

and then, in other method of other class, able this:

animal farmanimal; farmanimal = animals.sheep.getanimalclass().newinstance(); boolean shorn = farmanimal.isshorn(); 

(the value of shorn being false @ point);

farmanimal.shear(); shorn = farmanimal.isshorn(); 

(shorn == true)

farmanimal = animals.sheep.getanimalclass().newinstance(); shorn = farmanimal.isshorn(); 

(shorn == false)

obviously, isn't best way i've done here, that's not point. know can specify behaviour enum constants, doesn't make them multiply-instantiable distinct instances. want able create multiple instances (or copies) of various enum constants, different instance variables (and different quantities/types of instance variables), different accessor methods, can stuff (alter instance variables) without modifying enum constant.

i enum constants designed immutable. doesn't clash idea; want each enum constant represent immutable definition of mutable class.

you can this:

public enum animaltype { cow { @override public animal createnew() { return new cow(); } }, horse { @override public animal createnew() { return new horse(); } }; public abstract animal createnew(); } public abstract class animal { private final animaltype type; private final string namestring; public animal(final animaltype type, final string namestring) { super(); this.type = type; this.namestring = namestring; } public string getname() { return namestring; } public animaltype gettype() { return type; } } public class horse extends animal { public horse() { super(animaltype.horse, "horse"); } } public class cow extends animal { private boolean milk; public cow() { super(animaltype.cow, "cow"); } public boolean hasmilk() { return milk; } public void setmilk(final boolean milk) { this.milk = milk; } } @test public void testenum() { cow cow = (cow) animaltype.cow.createnew(); horse horse = (horse) animaltype.horse.createnew(); system.out.println("cow : " + cow.gettype() + ", has milk: " + cow.hasmilk()); system.out.println("horse: " + horse.gettype()); } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -