OnJava8-Examples/generics/DogsAndRobots.java

43 lines
897 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// generics/DogsAndRobots.java
2015-06-15 17:47:35 -07:00
// <20>2015 MindView LLC: see Copyright.txt
// No latent typing in Java
import typeinfo.pets.*;
import static com.mindviewinc.util.Print.*;
class PerformingDog extends Dog implements Performs {
@Override
public void speak() { print("Woof!"); }
@Override
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() {}
}
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);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Woof!
Sitting
Click!
Clank!
2015-09-07 11:44:36 -06:00
*/