2015-09-07 11:44:36 -06:00
|
|
|
|
// staticchecking/petspeak/PetSpeak.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 MindView LLC: see Copyright.txt
|
2015-06-15 17:47:35 -07:00
|
|
|
|
// Speaking pets in Java
|
|
|
|
|
package staticchecking.petspeak;
|
|
|
|
|
|
|
|
|
|
interface Pet {
|
|
|
|
|
void speak();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Cat implements Pet {
|
|
|
|
|
public void speak() {
|
|
|
|
|
System.out.println("meow!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Dog implements Pet {
|
|
|
|
|
public void speak() {
|
|
|
|
|
System.out.println("woof!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PetSpeak {
|
|
|
|
|
static void command(Pet p) { p.speak(); }
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Pet[] pets = { new Cat(), new Dog() };
|
|
|
|
|
for(Pet pet : pets)
|
|
|
|
|
command(pet);
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
|
meow!
|
|
|
|
|
woof!
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|