package com.mindprod.example;
import static java.lang.System.out;
/**
* Demonstrate use of Enum static and instance fields.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2009-03-24 - initial version
* @since 2009-03-24
*/
public class TestEnum
{
enum Animals
{
COW( 2 ),
GOAT( 2 ),
OSTRICH( 0 )
{
/**
OSTRICH overrides the default version of the method. */
int getWingCount()
{
return wc;
}
/**
field that only OSTRICH has */
final int wc = 2;
};
/**
* COW, GOAT and OSTRICH all share a single copy of this field.
*/
static int legs = 4;
/**
* even though COW, GOAT and OSTRICH all have this field, they each have their own copy.
*/
int horns;
Animals( int horns )
{
this.horns = horns;
}
int getHorns()
{
return horns;
}
void setHorns( int horns )
{
this.horns = horns;
}
int getLegs()
{
return legs;
}
/**
* default method, overridden by OSTRICH only.
*
* @return number of wings this animal has.
*/
int getWingCount()
{
return 0;
}
void setLegs( int legs )
{
Animals.legs = legs;
}
}
/**
* test enum
*
* @param args not used
*/
public static void main( String[] args )
{
out.println( Animals.COW.getHorns() );
Animals.COW.setHorns( 1 );
out.println( Animals.COW.getHorns() );
out.println( Animals.GOAT.getHorns() );
out.println( Animals.COW.getLegs() );
Animals.COW.setLegs( 5 );
out.println( Animals.COW.getLegs() );
out.println( Animals.GOAT.getLegs() );
out.println( Animals.OSTRICH.getWingCount() );
out.println( Animals.GOAT.getWingCount() );
}
}