29 lines
549 B
Java
29 lines
549 B
Java
|
//: staticchecking/petspeak/PetSpeak.java
|
||
|
// 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(int i = 0; i < pets.length; i++)
|
||
|
command(pets[i]);
|
||
|
}
|
||
|
} ///:~
|