[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();
}
}
}
}