如何将这段代码转换为我更熟悉的简单代码? [英] How do I transform this code into something simpler that I am more familiar with?

查看:111
本文介绍了如何将这段代码转换为我更熟悉的简单代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在是Java的第一年,在互联网上偶然发现了有关战舰游戏的这段代码。我想做的是用更简单的代码了解此代码。让我感到困惑的部分是第一行: computerGameBoard = new GameBoard(true,event->。我熟悉按钮以及它们具有动作事件的方式,但是这段代码与之相比又有何不同,是否有可能将其转向进入与按钮类似的动作事件吗?我可以将其拆分为另一种方法吗?预先感谢。还请注意,下面的代码行应附加到代码段上,但不应附加到代码段上。

I am currently in my first year of Java and have stumbled upon this code on the internet about a battleship game. What I would like to do is understand this code in more simple code. The part that confuses me is the first line: "computerGameBoard = new GameBoard(true, event ->". I am familiar with buttons and how they have action events, but how does this code compare to that and is it possible to turn this into a similar action event to a button? Could I split it into another method? Thanks in advance. Also note that the line of code below should be attached to the code segment, but its not.

    computerGameBoard = new GameBoard(true, event ->
    {
        if (!playing)
            return;

        BoardSquare boardsquare = (BoardSquare) event.getSource();
        if (boardsquare.wasHit)
            return;

        AIMove = !boardsquare.fire();
         System.out.println("enemyBoard shipcount:"+computerGameBoard.shipcount);
        if (computerGameBoard.shipcount == 0) {
            System.out.println("Winner!");

        }

        if (AIMove)
            AI1();
    });


推荐答案

没有lambda的onClickListener()示例:

An example of onClickListener() without lambda:

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // do something here
    }
});

可以用lambda重写:

can be rewritten with lambda:

mButton.setOnClickListener((View v) -> {
    // do something here
});

因此您的代码正在使用Lambda表达式(Java 8功能)

So your code is using Lambda expression (Java 8 feature)

这篇关于如何将这段代码转换为我更熟悉的简单代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆