创建一个由单元组成的板,可容纳通用值
我正在从事一个项目( A Game ),该项目将有一个值得注意的董事会。
我想让该板尽可能通用,以便能够重新使用其他可能的游戏代码。
public class Board {
private int rows;
private int cols;
private Box[][] board;
public Board(int rows, int cols){
Box[][] board = new Box[rows][cols];
for (int row = 0; row < this.rows; row++){
for (int col = 0; col < this.cols; col++){
board[row][col] = new Box();
}
}
}
public int getCols(){return this.cols;}
public int getRows(){return this.rows;}
public Piece getContentAtPos(int row, int col){
return board[row][col].getContent();
}
}
这是板
类。这里的问题是,我必须返回pique
用getContentatPos()
方法,因为box
( cell ) >)类是这样的:
public class Box {
private boolean empty;
private Piece content;
public Box(){
empty = true;
}
public Box(Piece piece){
empty = false;
this.content = piece;
}
public boolean isEmpty(){
return empty;
}
public Piece getContent(){
if (!empty) {
return content;
} else {
return null;
}
}
}
鉴于我的理想是类box
能够托管任何类型的对象并使用getContentAtpos()
返回此通用对象通过框getContent()
方法。
我的通用box
类。
public class Box<T>{
private boolean empty;
private T content;
public Box(){
empty = true;
}
public Box(T content){
this.content = content;
empty = false;
}
public boolean isEmpty(){
return empty;
}
public T getContent(){
if (!isEmpty()){
return content;
}
return null;
}
public T setContent(Object content){
this.content = content;
}
}
但是,我需要在董事会课程中进行什么更改?
我应该把什么回报价值放在那里?
I'm working on a project(a game) which will have a board to play on.
I want to make this board as generic as possible in order to be able to re-use the code for a possible other game.
public class Board {
private int rows;
private int cols;
private Box[][] board;
public Board(int rows, int cols){
Box[][] board = new Box[rows][cols];
for (int row = 0; row < this.rows; row++){
for (int col = 0; col < this.cols; col++){
board[row][col] = new Box();
}
}
}
public int getCols(){return this.cols;}
public int getRows(){return this.rows;}
public Piece getContentAtPos(int row, int col){
return board[row][col].getContent();
}
}
This is the Board
class. The problem here is that I have to return a Piece
object with the getContentAtPos()
method because the Box
(Cell) class is like this:
public class Box {
private boolean empty;
private Piece content;
public Box(){
empty = true;
}
public Box(Piece piece){
empty = false;
this.content = piece;
}
public boolean isEmpty(){
return empty;
}
public Piece getContent(){
if (!empty) {
return content;
} else {
return null;
}
}
}
Whereas, the ideal for me would be the class Box
to be able to host any type of object and return this generic object with getContentAtPos()
via the Box getContent()
method.
My generic Box
class.
public class Box<T>{
private boolean empty;
private T content;
public Box(){
empty = true;
}
public Box(T content){
this.content = content;
empty = false;
}
public boolean isEmpty(){
return empty;
}
public T getContent(){
if (!isEmpty()){
return content;
}
return null;
}
public T setContent(Object content){
this.content = content;
}
}
But what do I need to change in the Board class?
What return value shall I put there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您希望您的游戏作品会声明某种行为。如果是这样,
box
不需要存储任意对象,而是代表包含预期游戏的所有方法的类型对象。换句话说,您可以定义一个接口,假设
pique
,它将具有多个实现:您不需要使
box
类是通用的,而是可以包含一个类型pique
的字段:这使您可以自由地通过
box
class或true of the Setter提供peice
的任何实现。这意味着您可以设计给出只有一个实现myGamePiece
的核心部分,然后在添加更多类(例如newgamepeace
)上(名称应反映了类的目的)如果是清晰而简洁的方式,那只是虚拟的例子)。关键点是,您可以从并且不必更改先前设计和测试的代码即可使其与新类一起使用(称为 late binding )
和方法
getContentAtatpos()
还将返回类型pique
的对象。这是值得称赞的目标。
您的课程必须为 nofollow noreferrer“ https://en.wikipedia.org/wiki/grasp_(object-oriented_design)#high_cohesion“ rel =“ nofollow noreferrer”>狭窄的聚焦,以便重复使用。即每个类都应一组狭窄且定义明确的功能集,并仅注意那些对其工作至关重要的对象(减少耦合)。
而且您可以使必要的耦合松散(高耦合不好,但是类完全无法互动,可以通过引入界面,如上图所示。
即使您最终不会在其他项目中使用此代码,也将其松散耦合也是有益的,因为它使维护和扩展代码更加容易。
I guess you expect your game pieces to declare a certain behavior. If so,
Box
doesn't need to store an arbitrary object, but an object of type representing a contract that encompass all the methods which a game piece is expected to have.In other words, you can define an interface, let's say
Piece
, which will have several implementations:You don't need to make
Box
class to be generic, instead it can contain a field of typePiece
:That gives you a freedom to provide any implementation of the
Peice
via constructor of theBox
class or true the setter. It means you can design the core part of the gave having only one implementationMyGamePiece
, and later on add a few more classes likeNewGamePeace
(names should reflect the purpose of the class if the clear and concise way, it's just dummy example).The key point is that you can benefit from the polymorphism and don't have to change the previously designed and tested code in order to make it work with new classes (that called a late binding):
And method
getContentAtPos()
will also return an object of typePiece
.That's a laudable aim.
Your classes have to be loosely coupled and narrow focused in order to be reusable. I.e. each class should a narrow and well-defined set of functionality and be aware only of those object that are crucial for it work (reduce coupling).
And you can make the necessary coupling loose (high coupling isn't good, but classes can't interact without coupling at all, you need to maintain balance) by introducing interfaces as shown above.
Even if eventually you would not use this code in other project, keeping it loosely coupled is beneficial because it makes it easier to maintain and expand the code.
您应该创建未来将要返回的每个类都将实现的接口。这样,您将确保返回类型将实现接口并具有定义的行为。
You should create interface that every class you will return in the future will implement. This way you will be sure that return type will implement interface and have defined behaviour.