美文网首页
《Java编程思想》笔记-第五章

《Java编程思想》笔记-第五章

作者: 耶律枣 | 来源:发表于2018-09-11 15:19 被阅读0次

    构造器:

    构造器与类名相同。
    它是一种特殊类型的方法,没有返回值。

    class Rock {
      Rock() { // 无参构造器/默认构造器
        System.out.print("Rock ");
      }
      Rock(int i) {
        System.out.print("Rock " + i + " ");
      }
    }
    
    public class SimpleConstructor {
      public static void main(String[] args) {
        for(int i = 0; i < 10; i++)
          new Rock();
      }
      public static void main(String[] args) {
        for(int i = 0; i < 8; i++)
          new Rock2(i);
      }
    }
    

    输出:

    Rock Rock Rock Rock Rock Rock Rock Rock Rock Rock
    Rock 0 Rock 1 Rock 2 Rock 3 Rock 4 Rock 5 Rock 6 Rock 7
    

    区分方法重载:

    1.每个重载方法都要有独一无二的参数类型列表
    2.参数顺序不同
    返回值不能区分重载

    成员初始化:

    全局变量无需初始化,局部变量必须初始化

    public class InitialValues {
      boolean t;
      char c;
      byte b;
      short s;
      int i;
      long l;
      float f;
      double d;
      InitialValues reference;
      void printInitialValues() {
        System.out.println("Data type      Initial value");
        System.out.println("boolean        " + t);
        System.out.println("char           [" + c + "]");
        System.out.println("byte           " + b);
        System.out.println("short          " + s);
        System.out.println("int            " + i);
        System.out.println("long           " + l);
        System.out.println("float          " + f);
        System.out.println("double         " + d);
        System.out.println("reference      " + reference);
      }
    
      void f(){
          int i;
          // System.out.println(i); //Error, i未初始化
      }
      public static void main(String[] args) {
        InitialValues iv = new InitialValues();
        iv.printInitialValues();
        /* 也可以写成:
        new InitialValues().printInitialValues();
        */
      }
    }
    

    输出:

    Data type      Initial value
    boolean        false
    char           []
    byte           0
    short          0
    int            0
    long           0
    float          0.0
    double         0.0
    reference      null
    

    为变量赋值:

    public class InitialValues2 {
          boolean bool = true;//基本类型赋值
          char ch = 'x';
          byte b = 47;
          short s = 0xff;
          int i = 999;
          long lng = 1;
          float f = 3.14f;
          double d = 3.14159;
    
          InitialValues2 iv = new InitialValues2();//非基本类型赋值
          
          // 也可以调用某个方法来提供初值
          int i1 = f();
          int f(){
              return 11;
          }
          
          // 错误方式:程序的正确性取决于初始化的顺序
          // int j = g(a);
          // int a = f();
    }
    

    初始化顺序:

    在类的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布在方法定义之间,它们仍旧在任何方法(包括构造器)被调用之前得到初始化。

    class Window {
      Window(int marker) { System.out.println("Window(" + marker + ")"); }
    }
    
    class House {
      Window w1 = new Window(1);
      House() {
        System.out.println("House()");
        w3 = new Window(33); // Reinitialize w3
      }
      Window w2 = new Window(2);
      void f() { System.out.println("f()"); }
      Window w3 = new Window(3);
    }
    
    public class OrderOfInitialization {
      public static void main(String[] args) {
        House h = new House();
        h.f();
      }
    }
    

    输出:

    Window(1)
    Window(2)
    Window(3)
    House()
    Window(33)
    f()
    

    静态数据的初始化:

    静态数据只初始化一次,static关键字不能应用于局部变量,只能作用于域;静态的基本类型域,未初始化,那么它就会获得基本类型的标准初值,如果它是一个对象引用,那么它的默认初始值是null。

    class Bowl {
      Bowl(int marker) {
        print("Bowl(" + marker + ")");
      }
      void f1(int marker) {
        print("f1(" + marker + ")");
      }
    }
    
    class Table {
      static Bowl bowl1 = new Bowl(1);
      Table() {
        print("Table()");
        bowl2.f1(1);
      }
      void f2(int marker) {
        print("f2(" + marker + ")");
      }
      static Bowl bowl2 = new Bowl(2);
    }
    
    class Cupboard {
      Bowl bowl3 = new Bowl(3);
      static Bowl bowl4 = new Bowl(4);
      Cupboard() {
        print("Cupboard()");
        bowl4.f1(2);
      }
      void f3(int marker) {
        print("f3(" + marker + ")");
      }
      static Bowl bowl5 = new Bowl(5);
    }
    
    public class StaticInitialization {
      public static void main(String[] args) {
        print("Creating new Cupboard() in main");
        new Cupboard();
        print("Creating new Cupboard() in main");
        new Cupboard();
        table.f2(1);
        cupboard.f3(1);
      }
      static Table table = new Table();
      static Cupboard cupboard = new Cupboard();
    }
    

    输出:

    Bowl(1)
    Bowl(2)
    Table()
    f1(1)
    Bowl(4)
    Bowl(5)
    Bowl(3)
    Cupboard()
    f1(2)
    Creating new Cupboard() in main
    Bowl(3)
    Cupboard()
    f1(2)
    Creating new Cupboard() in main
    Bowl(3)
    Cupboard()
    f1(2)
    f2(1)
    f3(1)
    

    由输出课件,静态初始化只有在必要时刻才会进行。如果不创建Table对象,也不引用Table.b1或Table.b2,那么静态Bowl b1和b2永远都不会创建。只有在第一个Table对象被创建(或者第一次访问静态数据)时,他们才会被初始化。此后,静态对象不会再被初始化。
    初始化的顺序:先静态对象,然后是非静态对象。

    代码块:

    class Mug {
      Mug(int marker) {
        print("Mug(" + marker + ")");
      }
      void f(int marker) {
        print("f(" + marker + ")");
      }
    }
    
    public class Mugs {
      Mug mug1;
      Mug mug2;
      {
        mug1 = new Mug(1);
        mug2 = new Mug(2);
        print("mug1 & mug2 initialized");
      }
      Mugs() {
        print("Mugs()");
      }
      Mugs(int i) {
        print("Mugs(int)");
      }
      public static void main(String[] args) {
        print("Inside main()");
        new Mugs();
        print("new Mugs() completed");
        new Mugs(1);
        print("new Mugs(1) completed");
      }
    }
    

    结果:

    Inside main()
    Mug(1)
    Mug(2)
    mug1 & mug2 initialized
    Mugs()
    new Mugs() completed
    Mug(1)
    Mug(2)
    mug1 & mug2 initialized
    Mugs(int)
    new Mugs(1) completed
    

    执行顺序(优先级从高到低):
    静态代码块(只执行一次) > main方法 > 普通代码块(每次创建对象都会执行一次) > 构造方法

    相关文章

      网友评论

          本文标题:《Java编程思想》笔记-第五章

          本文链接:https://www.haomeiwen.com/subject/mlxugftx.html