setOfAnimalByClass.get(Giraffe.class).add(new Giraffe());
Scanner scanner = new Scanner(System.in); System.out.println("Name your giraffe."); String name = scanner.nextLine(); // get user input for name Giraffe giraffe = new Giraffe(name); // use the constructor above to set name System.out.println(giraffe.getName()); // prints name of giraffe
List<Animal> animals = new ArrayList<Animal>(); // List animals.Add(new Lion()); // Order 0 animals.Add(new Giraffe()); // Order 1 animals.Add(new Zebra()); // Order 2
class B<T> { private T thing; // I thought one could do generic parameter types in constructors as well? public B(T thing) { this.thing = thing; } } class C { public void whatever() { Animal f = new Giraffe(); addThing(f); } protected <K> void addThing(K object) { // if you need reflection, object.getClass() will be the K type at run time. process(new B<K>(object)); } // process method elided }
public class Main { public static void main(String[] args) { Zoo z1 = new Zoo("Zoo1"); Zoo z2 = new Zoo("Zoo2"); Animal pen1 = new Penguin("Luke"); Animal gir1 = new Giraffe("Duke"); Animal pen2 = new Penguin("Fluke"); Animal tig1 = new Tiger("Brooke"); pen1.setZoo(z1); pen2.setZoo(z2); gir1.setZoo(z2); tig1.setZoo(z2); System.out.println(pen1.belongsWhere()); System.out.println(tig1.belongsWhere()); System.out.println(z1); System.out.println(z2); } }
protected void testNotifyInvokedInternal() { reset(); Giraffe payload = new Giraffe(); fireEvent(payload); verifyObserversNotNotified(extension.getFiveMeterTallGiraffeObserver(), extension.getSixMeterTallAngryGiraffeObserver(), extension.getAngryNubianGiraffeObserver()); verifyObserversNotified(payload, extension.getAnyGiraffeObserver()); }