是的,使用多个较小的方法
我可以通过创建两种方法来解决吗,例如一种用于创建列表 boardIntegers,另一种用于更新 boardIntegers?
是的,你可以。你应该。最好将具有特定狭隘目的的小块代码分隔成命名良好的方法。
您将在下面的代码中看到我们将您的代码分解为两个主要方法:
哎呀
我认为您缺少的更大的图景是object-oriented programming (OOP)。
您的Board 类应该代表一个板,描述现实生活中的板。一块板具有特征/属性,在你的例子中,一个大小和一组整数(我猜它代表板上的插槽或位置)。所以你的类应该将这些属性表示为state,作为成员字段变量。
public class Board
{
// Member fields.
final private int boardSize;
final private List < Integer > slots;
…
在构造函数中准备好电路板状态的基础知识。通常最好使您的构造函数尽可能简短和简单。目标是做最少的事情,使对象保持连贯状态。
在我们这里的例子中,也许构造函数应该只使用默认的 1、2、3、... 顺序来建立槽。我们的构造函数采用一个参数,即大小 (50),因为这很可能是您可能更改的电路板的一个方面(我的假设)。
// Constructor
public Board ( final int boardSize )
{
this.boardSize = boardSize;
this.slots = new ArrayList <>( this.boardSize );
// Populate the slots with sequential numbers, one through count of slots.
for ( int nthSlot = 1 ; nthSlot <= this.boardSize ; nthSlot++ )
{
this.slots.add( nthSlot );
}
// Or more briefly, using streams:
// this.slots = new ArrayList <>( IntStream.rangeClosed( 1 , this.boardSize ).boxed().toList() );
}
您的目标是随机排列棋盘的插槽。所以创建一个方法randomize 来完成这项工作。
我质疑随机化代码的逻辑,但该代码与您的问题无关。因此,让我们跳过该代码,只需将每个插槽的值替换为调用 ThreadLocalRandom.current().nextInt 的结果即可。
private void randomize ( )
{
for ( int index = 0 ; index < this.boardSize ; index++ )
{
int absC = ThreadLocalRandom.current().nextInt( 1_000 );
this.slots.set( index , absC );
}
}
我们需要一种方法来检查我们的棋盘对象。所以我们添加了另一种方法,这个方法名为report。
private void report ( )
{
System.out.println( "this.slots = " + this.slots + " at " + Instant.now() );
}
? 回到问题的重点,注意对象的状态(成员字段boardSize & slots)是如何共享之间的各种方法。是的,方法可以将其结果打包为返回值。但在这种情况下,所有方法都集中在处理这一板对象上。所以我们让这些方法共享对成员字段的访问。我们让Board 方法直接操作Board 对象的字段(状态)。
还请注意,我们标记成员字段private 以拒绝外部代码的访问。只有Board 类上的方法才能操纵Board 对象的状态(通常)。其他类上的方法应该被拒绝访问(通常)。
让我们的对象跳舞的编排是main方法。
public static void main ( String[] args )
{
Board board = new Board( 50 );
board.report();
board.randomize();
board.report();
}
将所有代码放在一起。
package work.basil.game;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class Board
{
// Member fields.
final private int boardSize;
final private List < Integer > slots;
// Constructor
public Board ( final int boardSize )
{
this.boardSize = boardSize;
this.slots = new ArrayList <>( this.boardSize );
// Populate the slots with sequential numbers, one through count of slots.
for ( int nthSlot = 1 ; nthSlot <= this.boardSize ; nthSlot++ )
{
this.slots.add( nthSlot );
}
// Or more briefly, using streams.
// this.slots = new ArrayList <>( IntStream.rangeClosed( 1 , this.boardSize ).boxed().toList() );
}
// Logic
private void randomize ( )
{
for ( int index = 0 ; index < this.boardSize ; index++ )
{
int absC = ThreadLocalRandom.current().nextInt( 1_000 );
this.slots.set( index , absC );
}
}
private void report ( )
{
System.out.println( "this.slots = " + this.slots + " at " + Instant.now() );
}
public static void main ( String[] args )
{
Board board = new Board( 50 );
board.report();
board.randomize();
board.report();
}
}
运行时。
this.slots = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] 在 2021-12-17T23:09:04.872711Z
this.slots = [395, 335, 46, 691, 7, 799, 52, 354, 972, 127, 102, 767, 588, 126, 178, 762, 437, 895, 460, 222, 439, 316、150、445、963、878、776、820、263、457、259、513、639、102、176、828、738、866、6、540、996、511、576、52、85、833、 889、498、555、925] 在 2021-12-17T23:09:04.884395Z
顺便注意一下这里代码中的命名。遵循 Java 命名约定使代码更易于阅读。例如,变量和方法都以小写字母开头。所以,int AbsC; 应该是 int absC;。而UpdateBoard() 应该是updateBoard()。
具有多种方法的 OOP 使代码更易于理解
希望您能看到使用多种方法和 OOP 如何使这段代码更易于阅读和理解。 OOP 的发明是为了让人类程序员的生活更轻松,而不是为了计算机机器的利益。
- 查看该类的程序员可以浏览成员字段以了解该类的主要思想:持有一定数量的插槽。
- 然后看一眼构造函数就会发现槽的性质,默认为一系列数字。
- 然后查看
main 显示我们的意图是如何通过随机化这些插槽的值来更改电路板。
提示:IDEs 提供了一个“结构”面板来展示和导航这些方法,以便程序员快速查看。