一)在base包中
编写动物(Animal)接口,定义抽象方法
eat(),sleep(),show()
定义一个抽象类 哺乳动物 Mammal,实现Animal接口(但是暂不实现它的三个抽象方法),在这个抽象类中增加milk吃奶的抽象方法
定义一个抽象类 爬行动物 Reptile ,实现Animal接口(但是暂不实现它的三个抽象方法),在这个抽象类中增加crawl 爬行的抽象方法。
(二)在utility包中
定义 Person,Monkey,Cat,Dog,继承Mammal,实现其所有方法,每个类中定义两个属性,提供两个构造函数,提供打印输出的方法show(),输出动物的属性;
定义 Snake,Crocodile,继承Reptile,实现其所有方法,每个类中定义两个属性,提供两个构造函数。提供打印输出的方法show(),输出动物的属性。
(三)在javastudy包中,定义一个类Test1,
定义一个长度为6的Animal数组,分别存放以上的每一种动物,循环取出每个动物,调用其方法。要求所有方法都要被调用到【eat,sleep,show,milk,crawl】,其中要用到instanceof,把代码写到精简。
代码: //Animal.java package base; public interface Animal { public abstract void eat(); public abstract void sleep(); public abstract void show(); } //Mammal.java package base; public abstract class Mammal implements Animal{ public abstract void milk(); } //Reptile.java package base; public abstract class Reptile implements Animal { public abstract void crawl(); } //Cat.java package utility; import base.Mammal; public class Cat extends Mammal { private String species; private String keyLink; public Cat(){ species="Cat"; keyLink="Mammal"; } public Cat(String species,String keyLink){ this.species=species; this.keyLink=keyLink; } public void eat(){System.out.println(species+" is eating.");} public void sleep(){System.out.println(species+" is sleeping.");} public void milk(){System.out.println(species+" is drinking milk.");} public void show(){ System.out.println("species:"+species); System.out.println("key link:"+keyLink); } } //Dog.java package utility; import base.Mammal; public class Dog extends Mammal { private String species; private String keyLink; public Dog(){ species="Dog"; keyLink="Mammal"; } public Dog(String species,String keyLink){ this.species=species; this.keyLink=keyLink; } public void eat(){System.out.println(species+" is eating.");} public void sleep(){System.out.println(species+" is sleeping.");} public void milk(){System.out.println(species+" is drinking milk.");} public void show(){ System.out.println("species:"+species); System.out.println("key link:"+keyLink); } } //Monkey.java package utility; import base.Mammal; public class Monkey extends Mammal { private String species; private String keyLink; public Monkey(){ species="Monkey"; keyLink="Mammal"; } public Monkey(String species,String keyLink){ this.species=species; this.keyLink=keyLink; } public void eat(){System.out.println(species+" is eating.");} public void sleep(){System.out.println(species+" is sleeping.");} public void milk(){System.out.println(species+" is drinking milk.");} public void show(){ System.out.println("species:"+species); System.out.println("key link:"+keyLink); } } //Person.java package utility; import base.Mammal; public class Person extends Mammal { private String species; private String keyLink; public Person(){ species="Person"; keyLink="Mammal"; } public Person(String species,String keyLink){ this.species=species; this.keyLink=keyLink; } public void eat(){System.out.println(species+" is eating.");} public void sleep(){System.out.println(species+" is sleeping.");} public void milk(){System.out.println(species+" is drinking milk.");} public void show(){ System.out.println("species:"+species); System.out.println("key link:"+keyLink); } } //Crocodile.java package utility; import base.Reptile; public class Crocodile extends Reptile { private String species; private String keyLink; public Crocodile(){ species="Crocodile"; keyLink="Reptile"; } public Crocodile(String species,String keyLink){ this.species=species; this.keyLink=keyLink; } public void eat(){System.out.println(species+" is eating.");} public void sleep(){System.out.println(species+" is sleeping.");} public void crawl(){System.out.println(species+" is crawling.");} public void show(){ System.out.println("species:"+species); System.out.println("key link:"+keyLink); } } //Snake.java package utility; import base.Reptile; public class Snake extends Reptile { private String species; private String keyLink; public Snake(){ species="Snake"; keyLink="Reptile"; } public Snake(String species,String keyLink){ this.species=species; this.keyLink=keyLink; } public void eat(){System.out.println(species+" is eating.");} public void sleep(){System.out.println(species+" is sleeping.");} public void crawl(){System.out.println(species+" is crawling.");} public void show(){ System.out.println("species:"+species); System.out.println("key link:"+keyLink); } } //Test1.java package javastudy; import base.*; import utility.*; public class Test1 { public static void main(String args[]){ Animal[] animal=new Animal[]{new Cat(),new Crocodile(),new Dog(),new Monkey(),new Person(),new Snake()}; for(Animal i:animal){ i.eat(); i.sleep(); if(i instanceof Mammal){ Mammal m=(Mammal)i; m.milk(); } else if(i instanceof Reptile){ Reptile r=(Reptile)i; r.crawl(); } else {System.err.println("error!");} i.show(); System.out.println("--------------"); } } } 运行结果: Cat is eating. Cat is sleeping. Cat is drinking milk. species:Cat key link:Mammal -------------- Crocodile is eating. Crocodile is sleeping. Crocodile is crawling. species:Crocodile key link:Reptile -------------- Dog is eating. Dog is sleeping. Dog is drinking milk. species:Dog key link:Mammal -------------- Monkey is eating. Monkey is sleeping. Monkey is drinking milk. species:Monkey key link:Mammal -------------- Person is eating. Person is sleeping. Person is drinking milk. species:Person key link:Mammal -------------- Snake is eating. Snake is sleeping. Snake is crawling. species:Snake key link:Reptile --------------
好难呀!!!还不会