Created
January 13, 2015 13:04
-
-
Save alphamikevictor/74fda14170ee427a919d to your computer and use it in GitHub Desktop.
Ejemplo tonto de polimorfismo en Java
This file contains hidden or 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
| package com.alosfogones.instrument; | |
| public class Guitarra implements Instrument { | |
| @Override | |
| public void toca() { | |
| System.out.println("Soy una guitarra"); | |
| } | |
| } |
This file contains hidden or 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
| package com.alosfogones.instrument; | |
| public interface Instrument { | |
| public void toca(); | |
| } |
This file contains hidden or 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
| package com.alosfogones.instrument; | |
| public class Piano implements Instrument { | |
| @Override | |
| public void toca() { | |
| System.out.println("Soy un piano"); | |
| } | |
| } |
This file contains hidden or 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
| package com.alosfogones.runner; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Random; | |
| import com.alosfogones.instrument.Guitarra; | |
| import com.alosfogones.instrument.Instrument; | |
| import com.alosfogones.instrument.Piano; | |
| import com.alosfogones.instrument.Trompeta; | |
| public class Poly { | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| Random rand = new Random(); | |
| List<Instrument> instruments = new ArrayList<Instrument>(); | |
| Integer numberOfNotes = rand.nextInt(10); | |
| for(Integer i=0;i<numberOfNotes;i++){ | |
| Instrument instrument = null; | |
| Integer instrumentSelection = rand.nextInt(3); | |
| switch (instrumentSelection) { | |
| case 0: | |
| instrument = new Guitarra(); | |
| break; | |
| case 1: | |
| instrument = new Piano(); | |
| break; | |
| case 2: | |
| instrument = new Trompeta(); | |
| break; | |
| default: | |
| System.out.println("No se que hacer con " + instrumentSelection); | |
| break; | |
| } | |
| instruments.add(instrument); | |
| } | |
| // Now let's play | |
| for (Integer i=0;i<instruments.size();i++){ | |
| instruments.get(i).toca(); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| package com.alosfogones.instrument; | |
| public class Trompeta implements Instrument { | |
| @Override | |
| public void toca() { | |
| System.out.println("Soy una trompeta"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment