zoukankan      html  css  js  c++  java
  • Think in Java(二):初始化与清理

    1. 区分重载方法:

    參数顺序的不同能够区分两个方法,只是,普通情况下千万别这么做。由于这会使代码难以维护不能通过返回值类型来区分重载方法:由于假设我直接调用f(), 此时java不知道应该调用那一个

    public void f(){ 
    }
    public int f(){
    return 1;
    }

    2. 仅仅有当须要明白指出对当前对象的引用时, 才须要使用thiskeyword。比如, 当须要返回对当前对象的引用时,就经常在return语句这样写:

    public class Leaf {
    	private int i = 0;
    
    	public Leaf increment() {
    		i++;
    		return this;
    	}
    
    	void print() {
    		System.out.println("i = " + i);
    	}
    
    	public static void main(String[] args) {
    		Leaf x = new Leaf();
    		x.increment().increment().increment().print();
    	}
    } 
    // 因为increment()通过thiskeyword返回了对当前对象的引用。所以非常easy在一条语句对同一个对象运行多次操作


    3. 为什么须要finalize()方法?
    把一个对象用完后就“弃之不顾”的做法并不是总是安全的,当然。java有垃圾回收器负责回收无用对象占领的内存资源,但也有特殊情况:假定你的对象(并不是使用new)获得了一块“特殊”的内存区域,因为垃圾回收器仅仅知道释放那些经由new分配的内存。所以它不知道该怎样释放该对象的这块“特殊”内存。

    为了应对这样的情况,java同意在类中定义一个名为finalize()的方法

    不该将finalize()作为通用的清理方法



    之所以要有finalize(),是因为在分配内存时可能採用了类似C语言中的做法,而非Java中的通常做法。这样的噢概念情况主要发生在使用“本地方法”的情况下。本地方法是一种在Java中调用非Java代码的方式。

    不管是“垃圾回收”还是“终结方法”都不保证一定会发生,假设Java虚拟机并未面临内存耗尽的情形,它是不会浪费时间
    去运行垃圾回收以恢复内存的。

    4.初始化顺序:
    在类的内部,变量定义的先后顺序决定了初始化的顺序,即使变量定义散布于方法定义之间,他们仍然会在不论什么方法(包含构造器)被调用之前得到初始化。

    class Window {
    	Window(int marker) {
    		print("Window(" + marker + ")");
    	}
    }
    
    class House {
    	Window w1 = new Window(1); // Before constructor
    
    	House() {
    		// Show that we're in the constructor:
    		print("House()");
    		w3 = new Window(33); // Reinitialize w3
    	}
    
    	Window w2 = new Window(2); // After constructor
    
    	void f() {
    		print("f()");
    	}
    
    	Window w3 = new Window(3); // At end
    }
    
    public class OrderOfInitialization {
    	public static void main(String[] args) {
    		House h = new House();
    		h.f(); // Shows that construction is done
    	}
    } 
    /* Output:
    Window(1)
    Window(2)
    Window(3)
    House()
    Window(33)
    f()
    *///:~

    5. 静态数据初始化:
    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();
    } 
    /* Output:
    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)
    *///:~
    初始化的顺序是先静态对象, 而后是非静态对象,上面的类中要运行main()(静态方法)必须载入StaticInitialization,然后其静态域table和cupboard被初始化。这将导致它们相应的类也被载入,而且因为它们也都包括静态的Bowl对象,因此Bowl随后也被载入。


  • 相关阅读:
    C#对文件/目录的操作:Path、File、Directory、FileStream、StreamReader、StreamWriter等类的浅析
    我的SqlHelper类!
    Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!
    python脚本实现向钉钉群组发送消息
    pause模块
    ansible获取远程机器上的ip地址
    ansible 的file 模块
    find 查找文件
    docker 数据管理
    Docker 镜像 容器 仓库
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5347865.html
Copyright © 2011-2022 走看看