简体   繁体   English

Java:我如何让球像弹跳和弹性球一样?

[英]Java: How can I make a ball behave as a bouncing and elastic ball?

I have two classes BouncingBall and another one called ElasticBall . 我有两个类BouncingBall ,另一个叫做ElasticBall Both classes extends BallImpl which implements an interface called Ball . 这两个类都扩展了BallImpl ,它实现了一个名为Ball的接口。

public interface Ball {
    int DEFAULT_RADIUS = 50;

    int radius();
    Point center();
    void update();
}

public class BouncingBall extends BallImpl {
    public static final int MOVEMENT_SPEED = 12;

    static final int DOWN = 1;
    static final int UP = -1;

    private int direction;

    BouncingBall(int x, int y, int direction) {
        super(x, y);
        this.direction = direction;
    }

    @Override
    public void update() {
        direction = reverseDirectionIfNecessary();
        y = move();
    }

    private int reverseDirectionIfNecessary() {
        if (movingTooHigh() || movingTooLow()) {
            return switchDirection();
        }

        return this.direction;
    }

    private boolean movingTooLow() {
        return y + radius >= BallWorld.BOX_HEIGHT && movingDown();
    }

    private boolean movingTooHigh() {
        return y - radius <= 0 && movingUp();
    }

    private int switchDirection() {
        return movingDown() ? UP : DOWN;
    }

    private int move() {
        return y + (MOVEMENT_SPEED * direction);
    }

    private boolean movingDown() {
        return direction == DOWN;
    }

    private boolean movingUp() {
        return direction == UP;
    }
}

public class ElasticBall extends BallImpl {
    public static final int GROWTH_RATE = 2;

    static final int GROW = 1;
    static final int SHRINK = -1;

    private int growthDirection;

    ElasticBall(int x, int y, int radius, int growthDirection) {
        super(x, y, radius);
        this.growthDirection = growthDirection;
    }

    @Override
    public void update() {
        growthDirection = reverseGrowthDirectionIfNecessary();
        radius = next();
    }

    private int reverseGrowthDirectionIfNecessary() {
        if (growingTooBig() || shrinkingTooSmall()) {
            return switchDirection();
        }

        return this.growthDirection;
    }

    private boolean shrinkingTooSmall() {
        return radius <= 0 && shrinking();
    }

    private boolean growingTooBig() {
        return radius >= Ball.DEFAULT_RADIUS && growing();
    }

    private int switchDirection() {
        return growing() ? SHRINK : GROW;
    }

    private int next() {
        return radius + (GROWTH_RATE * growthDirection);
    }

    private boolean shrinking() {
        return growthDirection == SHRINK;
    }

    private boolean growing() {
        return growthDirection == GROW;
    }
}

I need to create a BouncingElasticBall which combines the behavior of the BouncingBall and the ElasticBall classes. 我需要创建一个BouncingElasticBall ,它结合了BouncingBallElasticBall类的行为。 I have poor knowledge in OOP, and I know Java does not allow multiple inheritance, so how can I solve this problem? 我对OOP知之甚少,而且我知道Java不允许多重继承,那么如何解决这个问题呢?

Thanks in advance. 提前致谢。

One way you could approach this is to not extend BallImpl , but make sort-of plugins . 你可以采用的一种方法是不扩展 BallImpl ,而是制作排序插件 Like this: 像这样:

public class BallImpl implements Ball {
    List<BallBehavior> behaviors = ...

    @Override
    public void update() {
       behaviors.forEach(behavior -> behavior.update(this));
    }
    ...
}

public interface BallBehavior {
    void update(BallImpl ballImpl);
}

And then, just write your elastic and bouncing logic as behaviors . 然后,将弹性和弹跳逻辑写成行为

Once you diverge hierarchies there's no way to merge them in java. 一旦你将层次结构分开,就无法在java中合并它们。

It's a design matter: if you know that ElasticBall and BouncingBall may be combined together, you should create two interfaces Elastic and Bouncing , both extending interface Ball , with common methods valid for both. 这是一个设计问题:如果您知道ElasticBallBouncingBall可以组合在一起,您应该创建两个接口ElasticBouncing ,两者都扩展接口Ball ,使用对两者都有效的常用方法。

Then the common method implementations may be set into a common abstract class, let's say AbstractBall . 然后可以将公共方法实现设置为公共抽象类,比如说AbstractBall At this point you can finally detail your three implementations: 此时,您最终可以详细说明您的三个实现:

  • ElasticBall extends AbstractBall implements Elastic
  • BouncingBall extendis AbstractBall implements Bouncing
  • ElasticBouncingBall extends AbstractBall implements Elastic, Bouncing

In this way you'll be able to control what to do in each method, reuse code for common stuff (in the abstract class). 通过这种方式,您将能够控制在每个方法中执行的操作,重用常见内容的代码(在抽象类中)。

You can use interfaces that allows multiple inheritance. 您可以使用允许多重继承的接口。 Make the interface for each ball ElasticBall and BouncingBall and implement both of them in BouncingElasticBall . 为每个球的界面ElasticBallBouncingBall和执行两者BouncingElasticBall

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM