·声明一个简单的类Performer1来代表实体表演。（ch5 目录）

1.定义Performer1类的类变量

 对于每一种表演都有一个固定的编号。因此用类变量来表示。

 同时用final变量来修饰类变量，确保变量值不被修改

2.定义代表马戏团的类Circus1

 声明类Circus1

 为Circus1类添加方法：public static void main (String [] args)

3.为类Circus1添加属性performer

4.利用类变量为属性performer赋值

 可以看到，不需要创建Performer1类，就可直接调用类变量Performer1.CLOWN。

5.在main（）方法中利用类变量来对Circus1的属性performer进行对比。

// Circus1.java

class Performer1

{

final static int ACROBAT = 1;

final static int CLOWN = 2;

final static int ELEPHANT = 3;

final static int LION = 4;

final static int MONKEY = 5;

}

class Circus1

{

private int performer;

public static void main (String [] args)

{

Circus1 c1 = new Circus1 ();

c1.performer = Performer1.CLOWN;

if (c1.performer == Performer1.ACROBAT)

System.out.println ("Acrobat is now in the main ring.");

else

if (c1.performer == Performer1.CLOWN)

System.out.println ("Clown is now in the main ring.");

else

if (c1.performer == Performer1.ELEPHANT)

System.out.println ("Elephant is now in the main ring.");

else

if (c1.performer == Performer1.LION)

System.out.println ("Lion is now in the main ring.");

else

System.out.println ("Monkey is now in the main ring.");

}

}

6.创建一个新的类Performer2来代表表演。

类变量不仅可以是基本类型，也可以是对象类型的。这里定义Performer2对象类型的类变量。

需要特别注意的是，对象类型的类变量必需调用new操作进行初始化。

7.创建一个新的类Circus2来代表马戏团。

与类Circus1唯一不同的是属性performer的类型为对象Performer2。

// Circus2.java

class Performer2

{

final static Performer2 ACROBAT = new Performer2 ();

final static Performer2 CLOWN = new Performer2 ();

final static Performer2 ELEPHANT = new Performer2 ();

final static Performer2 LION = new Performer2 ();

final static Performer2 MONKEY = new Performer2 ();

private Performer2 ()

{

}

}

class Circus2

{

private Performer2 performer;

public static void main (String [] args)

{

Circus2 c2 = new Circus2 ();

c2.performer = Performer2.CLOWN;

if (c2.performer == Performer2.ACROBAT)

System.out.println ("Acrobat is now in the main ring.");

else

if (c2.performer == Performer2.CLOWN)

System.out.println ("Clown is now in the main ring.");

else

if (c2.performer == Performer2.ELEPHANT)

System.out.println ("Elephant is now in the main ring.");

else

if (c2.performer == Performer2.LION)

System.out.println ("Lion is now in the main ring.");

else

System.out.println ("Monkey is now in the main ring.");

}

}