Last active
December 22, 2015 09:39
-
-
Save Lysander/6453562 to your computer and use it in GitHub Desktop.
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 parrots; | |
| public class Dog { | |
| private String name; | |
| public Dog(String name) { | |
| this.name = name; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public String sayHello() { | |
| return "Wuff! Wuff!"; | |
| } | |
| } |
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 parrots; | |
| public class DogAdapter extends Parrot { | |
| private Dog dog; | |
| public DogAdapter(Dog dog) { | |
| super(dog.getName()); | |
| this.dog = dog; | |
| } | |
| public String sayHello() { | |
| return this.dog.sayHello(); | |
| } | |
| } |
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 parrots; | |
| public class Parrot { | |
| private String name; | |
| public Parrot(String name) | |
| { | |
| this.name = name; | |
| } | |
| public String sayHello() { | |
| return String.format("Hello master, my name is %s", this.name); | |
| } | |
| } |
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 parrots; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class ParrotDemo { | |
| public static void main(String[] args) { | |
| List<Parrot> parrots = new ArrayList<Parrot>(); | |
| parrots.add(new Parrot("Charly")); | |
| parrots.add(new Parrot("Lorchen")); | |
| System.out.println("Hello, my parrots!"); | |
| greetMaster(parrots); | |
| DogAdapter dog = new DogAdapter(new Dog("Spike")); | |
| parrots.add(dog); | |
| System.out.println("Hello, my pets!"); | |
| greetMaster(parrots); | |
| } | |
| public static void greetMaster(List<Parrot> parrots) { | |
| for (Parrot parrot : parrots) { | |
| System.out.println(parrot.sayHello()); | |
| } | |
| System.out.println(); | |
| } | |
| } |
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
| #!/usr/bin/env python3 | |
| class Parrot: | |
| def __init__(self, name): | |
| self.name = name | |
| def say_hello(self): | |
| return "Hello master, my name is {}".format(self.name) | |
| class Dog: | |
| def __init__(self, name): | |
| self.name = name | |
| def say_hello(self): | |
| return "Wuff! Wuff!" | |
| def greet_master(pets): | |
| print(*(pet.say_hello() for pet in pets), sep="\n") | |
| print() | |
| def main(): | |
| parrots = [Parrot("Charly"), Parrot("Lorchen")] | |
| print("Hello, my parrots!") | |
| greet_master(parrots) | |
| # Sinnvoller Weise würde man den Container umbenennen, aber es soll ja | |
| # nur demonstrieren, dass man einen Hund sehr wohl in einen Container | |
| # von Papageien stecken kann | |
| parrots.append(Dog("Spike")) | |
| print("Hello, my pets!") | |
| greet_master(parrots) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment