Java Enum finalize()方法

java.lang.Enum.finalize() 方法显示枚举类不能有finalize方法。

1 语法

protected final void finalize()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Enum finalize()方法
 */
import java.lang.*;

// enum showing Mobile prices
enum Mobile {
   Samsung(400), Nokia(250);
  
   int price;
   Mobile(int p) {
      price = p;
   }
   int showPrice() {
      return price;
   } 
}

public class EnumDemo {

   public static void main(String args[]) {

     System.out.println("enum class cannot have finalize methods...");       
     EnumDemo t = new EnumDemo() {
        protected final void finalize() { }    
     }; 

     System.out.println("CellPhone List:");
     for(Mobile m : Mobile.values()) {
        System.out.println(m + " costs " + m.showPrice() + " dollars");
     }                    
   }
}

输出结果为:

enum class cannot have finalize methods...
CellPhone List:
Samsung costs 400 dollars
Nokia costs 250 dollars

 

热门文章

优秀文章