Created
September 18, 2020 14:35
-
-
Save sergiosvieira/f56527f610bdd8342ab1ac70cd9fc141 to your computer and use it in GitHub Desktop.
Semana 02 - Classe Abstrata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
abstract class ObjetoGeometrico { | |
public abstract void desenhar(); // método abstrato | |
public void posicao() { // método não-abstrato | |
System.out.print("Estou no centro da tela!"); | |
} | |
} | |
class Quadrado extends ObjetoGeometrico { | |
@Override | |
public void desenhar() { | |
System.out.println("Desenho de um quadrado!"); | |
} | |
} | |
class Triangulo extends ObjetoGeometrico { | |
@Override | |
public void desenhar() { | |
System.out.println("Desenho de um triângulo!"); | |
} | |
} | |
public class Tela { | |
Random r = new Random(); | |
public ObjetoGeometrico criar() { | |
ObjetoGeometrico obj; | |
int code = r.nextInt(); | |
if (code % 2 == 0) | |
obj = new Triangulo(); | |
else | |
obj = new Quadrado(); | |
return obj; | |
} | |
public static void main(final String[] args) { | |
final Tela tela = new Tela(); | |
ObjetoGeometrico obj; | |
for (int i=0; i < 5; ++i) { | |
obj = tela.criar(); | |
obj.desenhar(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment