zoukankan      html  css  js  c++  java
  • Java随记1

    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

  • 相关阅读:
    configbody
    add log to ldap
    registerComponent announceExist
    ldap
    6485 commands
    Titled Motor Entry
    ldap pkg
    remove rpm pkg
    创建自定义验证控件,以验证多行文本框中内容长度为例
    ASP.NET利用CustomValidator的ClientValidationFunction与OnServerValidate来double check资料输入的正确性
  • 原文地址:https://www.cnblogs.com/changweihua/p/2178169.html
Copyright © 2011-2022 走看看