实现一个接口对象
代码如下:
public interface Talk {
void talk();
}
public class Dog implements Talk {
@Override
public void talk() {
// TODO Auto-generated method stub
System.out.println("hello,我的叫声是汪汪");
}
}
public class Cat implements Talk {
@Override
public void talk() {
// TODO Auto-generated method stub
System.out.println("hello,我的叫声是喵喵");
}
}
public class TestTalk {
public static void main(String[] args) {
// TODO Auto-generated method stub
Talk dog = new Dog();
Talk cat = new Cat();
Talk sheep = new Sheep();
dog.talk();
cat.talk();
sheep.talk();
}
}
在创建一个对象时,用:Dog dog = new Dog();
dog.talk();
也可以调用,这和:Talk dog = new Dog();
dog.talk();
有什么不同?