Enum Types in java

本文详细介绍了Java中枚举类型的定义及使用方法,包括如何为枚举常量指定参数值,定义枚举构造器,以及如何实现枚举类型的方法来完成特定任务。通过两个实例,演示了枚举类型在实际应用中的灵活性。

java tutorial fourth edition 4.5

Enum Types

 

Each enum constant is declared with values for the mass and radius parameters. These values are passed to the constructor when the constant is created. Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon.

Note

The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.


public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7),
PLUTO (1.27e+22, 1.137e6);

private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; }

// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;

double surfaceGravity() {
return G * mass / (radius * radius);
}
double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
public static void main(String[] args) {
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
}

If you run Planet.class from the command line with an argument of 175, you get this output:

$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413
Your weight on PLUTO is 11.703031

--------------------------------------------------------------------------------

practice :


public enum FC {
 BOJAN ("Bojan", 27, 1990),
 SILVA ("Henry", 14, 1977),
 PUYOL ("Puyol", 5, 1978),
 XAVI ("Xavi", 6, 1980),
 INIESTA ("Iniesta", 8, 1984),
 MESSI ("Messi", 19, 1987);
 
 private String name;
 private int num;
 private int year;
 
 FC(String name, int num, int year){
  this.name = name;
  this.num = num;
  this.year = year;
 }
 
 int getAge(){
  return 2010 - year;
 }
 
 public static void main(String[] args){
  for(FC fc : FC.values()){
   System.out.println("Name :" + fc.name +
   "/tNumber :" + fc.num +
   "/tAge :" + fc.getAge());
  }
 }
}
--------------

Name :Bojan Number :27 Age :20
Name :Henry Number :14 Age :33
Name :Puyol Number :5 Age :32
Name :Xavi Number :6 Age :30
Name :Iniesta Number :8 Age :26
Name :Messi Number :19 Age :23

在大多数编程语言中,以下几种类型的变量通常不能或不适合在函数的形式参数声明中被声明为引用类型: ### 字面常量 字面常量代表固定的值,不能被修改,因此不能作为引用类型的参数传递。例如在 C# 中: ```csharp void ModifyValue(ref int num) { num = num + 1; } // 错误示例,不能将字面常量作为 ref 参数传递 // ModifyValue(ref 5); ``` ### 临时表达式结果 临时表达式的结果是一个临时值,没有稳定的存储地址,不能通过引用传递。例如在 Java 中: ```java class Main { static void modifyValue(int[] arr) { arr[0] = arr[0] + 1; } public static void main(String[] args) { // 错误示例,不能将临时表达式结果作为引用传递 // modifyValue(new int[]{1, 2, 3}[0]); } } ``` ### 不可变类型的子类实例(如 Java 中的 String 子类) 在 Java 中,`String` 是不可变类型,即使在某些情况下可能有 `String` 的子类,也不能通过引用修改其值,因此不适合作为引用类型参数。 ```java class Main { static void changeString(ref String str) { // 这里只是示意,Java 没有 ref 语法 str = "new value"; } public static void main(String[] args) { String s = "old value"; // 虽然 Java 没有 ref 语法,但即便有也不应该这么做,因为 String 不可变 // changeString(ref s); } } ``` ### 枚举常量 枚举常量是固定的值,不能被修改,所以不能作为引用类型参数。例如在 C# 中: ```csharp enum Colors { Red, Green, Blue } void ModifyColor(ref Colors color) { // 错误示例,枚举常量不能通过引用修改 // color = Colors.Yellow; } // 错误示例,不能将枚举常量作为 ref 参数传递 // ModifyColor(ref Colors.Red); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值