现在的位置: 首页 > 综合 > 正文

Java随记1

2013年01月31日 ⁄ 综合 ⁄ 共 1057字 ⁄ 字号 评论关闭

1、找出如下代码的错误
public class test {
 public test() {
 }

 test a;
 {
  a = new test();
 }
 static test s;
 static {
  s = new test();
 }

 public static void main(String[] args) {
  new test();
 }
}
错误:实例变量初始化的时候出问题  他那个属性是自己的类, 所以在创建的时候就会一直在那里循环
错误原理:静态变量实例了 一次就在内存中共享。 而实例变量每次都会分内存空间,每次都会创建实例
2、Java枚举
package test.test2;

public enum Trice {
 RED(1){
  public  Trice getNext(){
   try {
    Thread.sleep(super.a*1000);
    System.out.println("the thread sleep" + super.a);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   return GREEN.getNext();
  }
 },
 GREEN(2)
 {
  public  Trice getNext(){
   try {
    Thread.sleep(super.a*1000);
    System.out.println("the thread sleep" + super.a);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   return YELLOW.getNext();
  }
 },YELLOW(3)
 {
  public  Trice getNext(){
   try {
    Thread.sleep(super.a*1000);
    System.out.println("the thread sleep" + super.a);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   return RED.getNext();
  }
 };
 public int a;
 private Trice(int a){
  this.a=a;
 }
 public abstract Trice getNext();
}
Java枚举变量默认继承外层枚举类,所以实现父类方法
3、最快实现清除List重复项的方法:使用HashSet

抱歉!评论已关闭.