已知两个接口
public interface Animal {
}
public interface Life {
void shout();
}
Dog类实现这两个接口
public class Dog implements Animal,Life{
@Override
public void shout() {
System.out.println("hello world");
}
}
Cat只实现Animal这一个接口
public class Cat implements Animal{
}
Main接口
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Life dogLife = show(dog);
dogLife.shout();//hello world
// 以下情况会报错
// 虽然编译时没有报错,但运行时会报错
// Cat cat = new Cat();
// Life catLife = show(cat);
// catLife.shout();
}
public static Life show(Animal animal){
return (Life) animal;
}
}