Life at Shanghai

Smile All Day Long

博客园 首页 新随笔 联系 订阅 管理
Chapter 4 : Initialization & Cleanup

......跳过,跳过,第一句要提醒自己的是:一旦写下一个任意形式签名(signature)的构造函数,编译器将不会再提供自动生成default构造函数。

在构造函数中调用构造函数(Calling constructors from constructors)

需要注意的是super与this两种在构造函数调用构造函数的方式,必须是构造函数中的第一句语句。这也就以为着,一个构造函数中只能使用二者中的一个。
例如:

/*
 * Created on 2004-11-26
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

package com.smilereader.test;

/**
 * @author Sinbadblue Kong
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

public class Dragon {
    
private String name;
    
    
private int challengeRank;    
    
    
/**
     * @param name The name of a Dragon
     
*/

    
protected Dragon(String name){
        
this.name = name;
    }


    
/**
     * @param name
     * @param challengeRank
     
*/

    
protected Dragon(String name, int challengeRank) {
        super();
        
this.name = name;
        
this.challengeRank = challengeRank;
    }

    
/**
     * @return Returns the name.
     
*/

    String getName() 
{
        
return name;
    }

    
/**
     * @return Returns the challengeRank.
     
*/

    
int getChallengeRank() {
        
return challengeRank;
    }

}



/*
 * Created on 2004-11-26
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

package com.smilereader.test;

/**
 * @author Sinbadblue Kong
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

public class RedDragon extends Dragon {
    
    
/**
     * @param name
     * @param challengeRank
     
*/

    
public RedDragon(String name, int challengeRank) {
        super(name, challengeRank);
        
// TODO Auto-generated constructor stub
    }

    
/**
     * @param name The name of a RedDragon
     
*/

    
public RedDragon(String name) {
        
this(name,14);        
        
// TODO Auto-generated constructor stub
    }


}



/*
 * Created on 2004-11-26
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

package com.smilereader.test;

import junit.framework.TestCase;

/**
 * @author Sinbadblue Kong
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

public class RedDragonTest extends TestCase {

    
/*
     * Class under test for void RedDragon(String, int)
     
*/

    
public void testRedDragonStringint() {
        RedDragon rd 
= new RedDragon("Magi",16);
        assertEquals(
"Magi",rd.getName());
        assertEquals(
16,rd.getChallengeRank());
    }


    
/*
     * Class under test for void RedDragon(String)
     
*/

    
public void testRedDragonString() {
        RedDragon rd 
= new RedDragon("Fireball");
        assertEquals(
"Fireball",rd.getName());
        assertEquals(
14,rd.getChallengeRank());
    }


}



Cleanup

首先,Java的finalize( )不是C++中的析构函数。有一点就足以区分它们,finalize( )不会在一个对象无效(即不存在reference指向它)时自动调用。finalize( )发生作用是在Java的垃圾回收器处理时调用的,具体步骤是这样:当垃圾回收器打算回收某个对象的内存空间时,首先会调用其finalize( )函数。而再下一次的垃圾回收动作时才真正回收这个对象的内存空间。因为垃圾回收器何时会处理是无法确定的,所以也就不可能依赖finalize( )来完成部分非内存空间的cleanup。例如,与数据库的连接。这时,就需要自行在需要的时候调用某些函数来完成这些cleanup操作。这点和C++的析构函数相比似乎就少了些便利性。

The termination condition 终止条件

Bruce 在TIJ中提到了一种finalize( )的用法,称为终止条件。因为在对象被垃圾回收器回收前,必定会被其调用finalize( )。由此,可以在finalize( )中加入判断对象的终止条件以对于程序的逻辑正确性做判断。不过,在我看来这个实在是太不实用了。想想,垃圾回收动作甚至可能发生在程序结束后,如果内存远远空闲的话。为此,必须用System.gc( ) 强迫终结动作。即使就Bruce所说的:你应该在程序开发过程中进行此事,以便于debugging。书上的例子如下:
//: c04:TerminationCondition.java
// Using finalize() to detect an object that
// hasn't been properly cleaned up.
import com.bruceeckel.simpletest.*;

class Book {
  boolean checkedOut 
= false;
  Book(boolean checkOut) 
{
    checkedOut 
= checkOut;
  }

  
void checkIn() {
    checkedOut 
= false;
  }

  
public void finalize() {
    
if(checkedOut)
      System.
out.println("Error: checked out");
  }

}


public class TerminationCondition {
  
static Test monitor = new Test();
  
public static void main(String[] args) {
    Book novel 
= new Book(true);
    
// Proper cleanup:
    novel.checkIn();
    
// Drop the reference, forget to clean up:
    new Book(true);
    
// Force garbage collection & finalization:
    System.gc();
    monitor.expect(
new String[] {
      
"Error: checked out"}
, Test.WAIT);
  }

}
 ///:~

posted on 2004-11-26 11:19  Sinbadblue's Blog  阅读(313)  评论(0)    收藏  举报