2015-04-20 15:36:01 -07:00
|
|
|
|
//: generics/DogsAndRobots.java
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
// No latent typing in Java
|
|
|
|
|
import typeinfo.pets.*;
|
|
|
|
|
import static net.mindview.util.Print.*;
|
|
|
|
|
|
|
|
|
|
class PerformingDog extends Dog implements Performs {
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public void speak() { print("Woof!"); }
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public void sit() { print("Sitting"); }
|
|
|
|
|
public void reproduce() {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Robot implements Performs {
|
|
|
|
|
public void speak() { print("Click!"); }
|
|
|
|
|
public void sit() { print("Clank!"); }
|
|
|
|
|
public void oilChange() {}
|
2015-05-18 23:05:20 -07:00
|
|
|
|
}
|
2015-04-20 15:36:01 -07:00
|
|
|
|
|
|
|
|
|
class Communicate {
|
|
|
|
|
public static <T extends Performs>
|
|
|
|
|
void perform(T performer) {
|
|
|
|
|
performer.speak();
|
|
|
|
|
performer.sit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DogsAndRobots {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
PerformingDog d = new PerformingDog();
|
|
|
|
|
Robot r = new Robot();
|
|
|
|
|
Communicate.perform(d);
|
|
|
|
|
Communicate.perform(r);
|
|
|
|
|
}
|
|
|
|
|
} /* Output:
|
|
|
|
|
Woof!
|
|
|
|
|
Sitting
|
|
|
|
|
Click!
|
|
|
|
|
Clank!
|
|
|
|
|
*///:~
|