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");}
}
對象,這是我的層次鑄造上實現無關的接口的Java
吃,Omnivora無關接口
哺乳實現多吃接口
Ursus的延伸哺乳實現Omnivora接口
貓科動物延伸哺乳動物
因爲它可以看到omnivora和吃是不相關的接口,但是1號線和2號線分別打印「實例吃」和「實例omnivora」。
有人能告訴我爲什麼嗎?
請記住,接口名稱和類名應該以大寫字母開頭;不是小寫。 – blackpanther
因爲「熊熊延伸哺乳動物實現Omnivora接口」(哺乳動物實現吃) –