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

  • 相关阅读:
    【Linux学习七】软件安装
    【Linux学习六】用户管理
    【Linux学习五】文本处理
    【Linux学习四】正则表达式
    【Linux学习三】VI/VIM全屏文本编辑器
    【Linux学习二】文件系统
    【Linux学习一】命令查看与帮助
    【安装虚拟机四】设置快照和克隆
    【安装虚拟机三】设置Linux IP地址
    SpringBoot之定时任务详解
  • 原文地址:https://www.cnblogs.com/changweihua/p/2178169.html
Copyright © 2011-2022 走看看