1个回答
2013-03-31
展开全部
可以的,给你写个例子
interface Speak {
void speak();
}
class Cat implements Speak {
public void speak() { System.out.println("meow"); }
}
class Dog implements Speak {
public void speak() { System.out.println("woof"); }
}
class Bird implements Speak {
public void speak() { System.out.println("chirrup"); }
}
class test
{
public static void main(String[] args) {
Speak[] pets = new Speak[]{ new Cat(), new Dog(), new Bird() };
for(Speak pet : pets)
pet.speak();
}
}
interface Speak {
void speak();
}
class Cat implements Speak {
public void speak() { System.out.println("meow"); }
}
class Dog implements Speak {
public void speak() { System.out.println("woof"); }
}
class Bird implements Speak {
public void speak() { System.out.println("chirrup"); }
}
class test
{
public static void main(String[] args) {
Speak[] pets = new Speak[]{ new Cat(), new Dog(), new Bird() };
for(Speak pet : pets)
pet.speak();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询