Java隨記1

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.