?? terminationconditionex.java
字號:
// initialization/TerminationConditionEx.java
// TIJ4 Chapter Initialization, Exercise 10, page 177
/* Create a class with a finalize() method that prints a message. In main(),
* create an object of your class. Explain the behavior of your program.
*/
class WebBank {
boolean loggedIn = false;
WebBank(boolean logStatus) {
loggedIn = logStatus;
}
void logIn() {
loggedIn = true;
}
void logOut() {
loggedIn = false;
}
protected void finalize() {
if(loggedIn)
System.out.println("Error: still logged in");
// Normally, you'll also do this:
// super.finalize(); // Call the base-class version
}
}
public class TerminationConditionEx {
public static void main(String[] args) {
WebBank bank1 = new WebBank(true);
WebBank bank2 = new WebBank(true);
// Proper cleanup: log out of bank1 before going home
bank1.logOut();
// Drop the reference, forget to cleanup:
new WebBank(true);
// Force garbage collection and finalization:
System.gc();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -