Created
September 18, 2020 13:58
-
-
Save sergiosvieira/b187f7b6d41b264a6a0f1cbefa8f7388 to your computer and use it in GitHub Desktop.
Semana 02 - Polimorfismo
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.*; | |
class Veiculo { | |
public void buzinar() { | |
System.out.println("Buzina não instalada!:("); | |
} | |
} | |
class Automovel extends Veiculo { | |
public void buzinar() { // Método Polimórfico | |
System.out.println("Beep Beep!"); | |
} | |
} | |
class Bicicleta extends Veiculo { | |
public void buzinar() { // Método Polimórfico | |
System.out.println("Foom Foom!"); | |
} | |
} | |
public class Oficina { | |
Random r = new Random(); | |
public Veiculo proximo() { | |
Veiculo v; | |
int code = r.nextInt(); | |
if (code%2 == 0) | |
v = new Automovel(); | |
else | |
v = new Bicicleta(); | |
return v; | |
} | |
public static void main(String[] args) { | |
Oficina o = new Oficina(); | |
Veiculo v; | |
for (int i=0; i<4; ++i) { | |
v = o.proximo(); | |
v.buzinar(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment