本帖最后由 ab8846254 于 2017-6-23 21:42 编辑
PS:(写的不是太好)高手就别看了
(一)在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,把代码写到精简。
阅读以下以下文档,了解instanceof 关键字的用法,在循环中进行类型转换,调用Mammal和Reptile特有的方法。
instanceof的用法请参照下面的文档【参考文档:(一定要看)】
http://rodney.cnblogs.com/archive/2005/08/18/instanceof.html
http://blog.csdn.net/jal198958/article/details/8251465
[Java] 纯文本查看 复制代码 package base;
//接口
public interface Animal {
//吃饭的抽象方法
public abstract void eat();
//睡觉的抽象方法
public abstract void sleep();
//show方法
public abstract void show();
}
package base;
//哺乳动物
public abstract class Mammal implements Animal {
//吃奶的抽象方法
public abstract void milk();
}
package base;
//爬行动物抽象类
public abstract class Reptile implements Animal{
//爬行的抽象方法
public abstract void crawl();
}
package utility;
import base.Mammal;
//猫 哺乳动物
public class Cat extends Mammal{
private static String name;
private static int age;
public Cat() {
super();
}
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Cat.name = name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
Cat.age = age;
}
@Override
public void eat() {
System.out.println("猫要吃鱼");
}
@Override
public void sleep() {
System.out.println("猫趴着睡觉");
}
@Override
public void milk() {
System.out.println("猫也要吃奶");
}
@Override
public void show(){
System.out.println("姓名:"+Cat.getName());
System.out.println("年龄:"+Cat.getAge());
}
public static void fuhao(){
System.out.println("-------------------");
}
}
package utility;
import base.Reptile;
public class Crocodile extends Reptile{
private static String name;
private static int age;
public Crocodile() {
super();
// TODO 自动生成的构造函数存根
}
public Crocodile(String name,int age){
this.name=name;
this.age=age;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Crocodile.name = name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
Crocodile.age = age;
}
@Override
public void eat() {
System.out.println("鳄鱼吃小鱼");
}
@Override
public void sleep() {
System.out.println("鳄鱼趴着睡觉");
}
@Override
public void crawl() {
System.out.println("鳄鱼四肢爬行");
}
@Override
public void show(){
System.out.println("姓名:"+Crocodile.getName());
System.out.println("年龄:"+Crocodile.getAge());
}
public static void fuhao(){
System.out.println("-------------------");
}
}
package utility;
import base.Mammal;
//狗 哺乳动物
public class Dog extends Mammal{
private static String name;
private static int age;
public Dog() {
}
public Dog(String name,int age){
Dog.name=name;
Dog.age=age;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Dog.name = name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
Dog.age = age;
}
@Override
public void eat() {
System.out.println("狗要吃骨头");
}
@Override
public void sleep() {
System.out.println("狗趴着睡觉");
}
@Override
public void milk() {
System.out.println("狗也要吃奶");
}
@Override
public void show(){
System.out.println("姓名:"+Dog.getName());
System.out.println("年龄:"+Dog.getAge());
}
public static void fuhao(){
System.out.println("-------------------");
}
}
package utility;
import base.Mammal;
//猴子 哺乳动物
public class Monkey extends Mammal{
private static String name;
private static int age;
public Monkey() {
super();
}
public Monkey(String name, int age) {
super();
this.name = name;
this.age = age;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Monkey.name = name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
Monkey.age = age;
}
@Override
public void eat() {
System.out.println("猴子要吃香蕉");
}
@Override
public void sleep() {
System.out.println("猴子挂着睡觉");
}
@Override
public void milk() {
System.out.println("猴子也要吃奶");
}
@Override
public void show(){
System.out.println("姓名:"+Monkey.getName());
System.out.println("年龄:"+Monkey.getAge());
}
public static void fuhao(){
System.out.println("-------------------");
}
}
package utility;
import base.Mammal;
//人类 哺乳动物
public class Person extends Mammal{
private static String name;
private static int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void eat() {
System.out.println("人类要吃饭");
}
@Override
public void sleep() {
System.out.println("人类要睡觉");
}
@Override
public void milk() {
System.out.println("人类要喝牛奶");
}
public static void fuhao(){
System.out.println("-------------------");
}
@Override
public void show(){
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
}
}
package utility;
import base.Reptile;
//蛇 爬行动物
public class Snake extends Reptile{
private static String name;
private static int age;
public Snake() {
super();
// TODO 自动生成的构造函数存根
}
public Snake(String name,int age){
this.name=name;
this.age=age;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Snake.name = name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
Snake.age = age;
}
@Override
public void eat() {
System.out.println("蛇吃老鼠");
}
@Override
public void sleep() {
System.out.println("蛇盘着睡觉");
}
@Override
public void crawl() {
System.out.println("蛇以S形爬行");
}
@Override
public void show(){
System.out.println("姓名:"+Snake.getName());
System.out.println("年龄:"+Snake.getAge());
}
public static void fuhao(){
System.out.println("-------------------");
}
}
package javastudy;
import base.Animal;
import base.Mammal;
import base.Reptile;
import utility.Cat;
import utility.Crocodile;
import utility.Dog;
import utility.Monkey;
import utility.Person;
import utility.Snake;
//测试类
public class Test1 {
public static void main(String[] args) {
Animal[] animals = new Animal[]{new Person("张三",25),new Monkey("孙悟空",500),new Cat("波斯猫",30),new Dog("哈士奇",5),new Snake("白娘子",5000),new Crocodile("鳄鱼",30)};
for(int x =0; x<animals.length;x++){
animals[x].show();
animals[x].eat();
animals[x].sleep();
if(animals[x] instanceof Mammal ){
Mammal m = (Mammal)animals[x];
m.milk();
Person.fuhao();
}else if (animals[x] instanceof Reptile){
Reptile r =(Reptile)animals[x];
r.crawl();
Crocodile.fuhao();
}
}
}
}
|