oao_e03_retangulo_finalize

Run Settings
LanguageJava
Language Version
Run Command
// @padawandr class Main { public static void main(String[] args) throws InterruptedException { // referencias para objetos do tipo Retangulo Retangulo r1, r2; Triangulo t1; // instanciar objetos r1 = new Retangulo(); r2 = new Retangulo(2, 3); t1 = new Triangulo(3, 4); System.out.println("Area R1 = " + r1.calcularArea()); System.out.println("Perimetro R1 = " + r1.calcularPerimetro()); System.out.println("Area R2 = " + r2.calcularArea()); System.out.println("Perimetro R2 = " + r2.calcularPerimetro()); System.out.println("Area T1 = " + t1.calcularArea()); // chamada explicita ao coletor de lixo r2 = null; System.gc(); // esperar 3 segundos e imprimir Thread.sleep(3000); // necessita do "throws InterruptedException" System.out.println("Fim do programa"); } }
// @padawandr public class Retangulo { float base; float altura; public Retangulo() { // nao precisa declarar este construtor! // chamar metodo construtor padrao, // onde (this.base = null) e (this.altura = null) } public Retangulo(float base, float altura) { this.base = base; this.altura = altura; } protected void finalize() throws Throwable { // executado antes do coletor de lixo destruir o objeto System.out.println("Estado do objeto a ser destruido:"); System.out.println("Base = " + this.base); System.out.println("Altura = " + this.altura); } public float calcularArea() { return (this.base * this.altura); } public float calcularPerimetro() { return 2 * (this.base + this.altura); } }
// @padawandr public class Triangulo { float altura; float base; public Triangulo(float altura, float base) { this.altura = altura; this.base = base; } public float calcularArea() { return (this.base * this.altura) / 2; } }
Editor Settings
Theme
Key bindings
Full width
Lines