zoukankan      html  css  js  c++  java
  • Class对象

    package typeinfo;//: typeinfo/SweetShop.java
    // Examination of the way the class loader works.
    import static net.mindview.util.Print.*;
    
    class Candy {
      static { print("Loading Candy"); }
    }
    
    class Gum {
      static { print("Loading Gum"); }
    }
    
    class Cookie {
      static { print("Loading Cookie"); }
    }
    
    public class SweetShop {
      public static void main(String[] args) {    
        print("inside main");
        new Candy();
        print("After creating Candy");
        try {
          Class.forName("Gum");
        } catch(ClassNotFoundException e) {
          print("Couldn't find Gum");
        }
        print("After Class.forName("Gum")");
        new Cookie();
        print("After creating Cookie");
      }
    } /* Output:
    inside main
    Loading Candy
    After creating Candy
    Loading Gum
    After Class.forName("Gum")
    Loading Cookie
    After creating Cookie
    *///:~

    这里的每一个类都有一个static,当类被加载时自动执行static的代码

     Class.forName("Gum");这个语句也会执行Gum这个类中的代码

  • 相关阅读:
    函数响应式编程
    RxSwift
    Swift 5.1 新语法
    MVVM
    SwiftUI 九
    SwiftUI 八
    SwiftUI制作View可嵌套组件
    Swift 5.1 新语法
    MVVM
    Linux系统修改hostname,不用重启
  • 原文地址:https://www.cnblogs.com/itchenfirst/p/8728644.html
Copyright © 2011-2022 走看看