class inher1
{
public static void main(String...args)
{
eat i = new ursus();
if(i instanceof eat) System.out.println("Instance of eat");
//Line 1
if((omnivora)i instanceof eat) System.out.println("Instance of eat");
//Line 2
if((omnivora)i instanceof omnivora) System.out.println("Instance of omnivora");
if(((omnivora)i).like_honey)System.out.println("Like Honey Obtained");
}
}
interface eat //Interface 1
{
public void eat();
}
interface omnivora //Interface 2
{
final public static boolean like_honey = true;
}
abstract class mammalia implements eat
{
abstract public void makenoise();
public void eat(){System.out.println("Yummy");}
}
class ursus extends mammalia implements omnivora
{
public void makenoise(){System.out.println("Growl");}
}
class felidae extends mammalia
{
public void makenoise(){System.out.println("Roar");}
}
这是我的层次结构
Eat 和 Omnivora 是不相关的接口
Mammalia 实现 Eat 接口
Ursus 扩展 Mammalia 实现 Omnivora 接口
猫科动物延伸哺乳动物
可以看出,omnivora 和 eat 是不相关的接口,但第 1 行和第 2 行都分别打印了“Instance of eat”和“Instance of omnivora”。
有人能告诉我为什么吗?