Utwórz klasę o nazwie Tank() obsłuhgującą operację fill() i empty() wyposażoną w warunek zakończenia wymagający opróżnienia obiektu przed usunieciem. Napisz metode finalize() ktora bedzie sprawdzac stan zbiornika. W metodzie main() sprawdz mozliwe scenariusze używania obiektów klasy Tank.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tank;
/**
*
* @author Nowy
*/
public class Tank {
static int counter;
int id = counter++;
boolean full;
public void Tank (){
System.out.println("Tank: "+ id + "created");
full = true;
}
public void empty (){full = false;}
protected void finalize (){
if (full)
System.out.println("Tank number "+id+"is not empty, clean it up first");
else
System.out.println("Tank number "+id+"has been deleted");
}
// public String toString(){return "Tank "+id;}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Tank().empty();
new Tank();
System.gc();
System.runFinalization();
}
}
//Wynik:
//Tank number 1has been deleted
//Tank number 0has been deleted
//a jeden nie powinien, porszę o wskazanie błędu.