java写动物类_用java写出猫狗鱼三个类的代码

这是我的个人想法,可以相互交流一下。使用继承实现动物的属性和构造方法,然后使用接口实现相应的功能。代码如下:

package com.fefjay.animal;

public class Animal {

/*

* 动物类:指定动物的属性,还有构造方法

*/

class MyAnimal {

private String name;

private String furColor;

private boolean hasFur;

private int num;

public MyAnimal() {

}

public MyAnimal(String name) {

this.name = name;

}

public MyAnimal(int num) {

this.num = num;

}

public MyAnimal(String name, int num) {

this.name = name;

this.num = num;

}

public MyAnimal(String name, boolean hasFur, String furColor) {

this.name = name;

this.hasFur = hasFur;

if (hasFur) {

this.furColor = furColor;

} else {

furColor = null;

}

}

public String getFurColor() {

return furColor;

}

public void setFurColor(String furColor) {

if (hasFur) {

this.furColor = furColor;

} else {

this.furColor = null;

}

}

public boolean isHasFur() {

return hasFur;

}

public void setHasFur(boolean hasFur) {

this.hasFur = hasFur;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

}

/*

* 动物行为接口:实现该接口表示动物有什么样的功能

*/

interface AnimalBehavior {

public void move();

public void eat();

}

/*

* 猫

*/

class Cat extends MyAnimal implements AnimalBehavior {

public Cat() {

}

public Cat(String color) {

super("cat", true, color);

}

public Cat(int num) {

super("cat", num);

}

@Override

public void move() {

// TODO Auto-generated method stub

}

@Override

public void eat() {

// TODO Auto-generated method stub

}

}

/*

* 狗

*/

class Dog extends MyAnimal implements AnimalBehavior {

public Dog() {

}

public Dog(String color) {

super("Dog", true, color);

}

public Dog(int num) {

super("Dog", num);

}

@Override

public void move() {

// TODO Auto-generated method stub

}

@Override

public void eat() {

// TODO Auto-generated method stub

}

}

/*

* 鱼

*/

class Fish extends MyAnimal implements AnimalBehavior {

public Fish() {

}

public Fish(int num) {

super("Fish", num);

}

@Override

public void move() {

// TODO Auto-generated method stub

}

@Override

public void eat() {

// TODO Auto-generated method stub

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值