繁体   English   中英

我可以“扩展”一个方法,或者在另一个方法内部调用一个方法吗?

[英]Can I “extend” a method, or call a method inside of another one?

*我正在开发一种在棋盘上只有棋子的游戏。 棋子可以移动,也可以拿走对手的棋子。 移动它向前移动一格,将它向前移动一格,向一侧移动。 由于移动动作也是每个采取动作的一部分,我想知道我是否可以以某种方式将这两者结合起来。 我试图将 move 方法集成到我的 take 方法中,但它不起作用,据说是因为该方法不知道它与哪个片段相关联。 这两种方法都会改变我的作品的属性(x 坐标和 y 坐标)以标记它在棋盘上的位置。 我可以用其他方式吗?

    /*
     * move action
     * differentiates between Black and White pieces, so both use the same method to move
     */
    public void move() {

        switch(color) {
        case "White":
            this.yCo = this.yCo+1;
            break;
        case "Black":
            this.yCo = this.yCo-1; 
            break;
        }

    }

    /*
     * take action
     * moves one tile forward and one tile to either left or right and removes an opponents piece on that tile
     */
    public void takeRight() {
        
        this.move();
        
        this.xCo = this.xCo+1;

    }
```*
public void move(int xShift) {
    switch(color) {
        case "White":
            this.yCo = this.yCo+1;
            break;
        case "Black":
            this.yCo = this.yCo-1; 
            break;
    }
    this.xCo += xShift;
}

this.xCo += xShiftthis.xCo = this.xCo + xShift的缩写。 你也可以去掉这个

暂无
暂无

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

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