简体   繁体   English

Java参考和新关键字

[英]Java reference and new keyword

I'm writing a simple chessboard while learning Java and come up with the following question: 我在学习Java时写了一个简单的棋盘,并提出了以下问题:

I have abstract class ChessPiece and classes like King , Queen , Rook etc. which all extend ChessPiece . 我有抽象类ChessPiece和类,如KingQueenRook等,它们都扩展了ChessPiece

And I have a 2D array of ChessPiece for the chessboard. 我有一个棋盘的二维ChessPiece阵列。

public ChessPiece myChessBoard[][] = new ChessPiece[8][8];

Then I make my chess pieces like this: 然后我像这样制作我的棋子:

ChessPiece aPiece = new Rook();

And put it on myChessBoard so I can find it: 把它放在myChessBoard上,这样我就可以找到它:

myChessBoard[0][0] = aPiece;

Same goes for other pieces... 其他作品也一样......

My question is when I do this public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; 我的问题是当我做这个public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; in my first step, I used new keyword and allocated 8*8 = 64 ChessPiece objects right? 在我的第一步中,我使用了新关键字并分配了8 * 8 = 64个ChessPiece对象吗? But I did it again when writing ChessPiece aPiece = new Rook(); 但是我在写ChessPiece aPiece = new Rook();时再次这样做了ChessPiece aPiece = new Rook(); I feel stuff in myChessBoard such as myChessBoard[0][0] don't need to be allocated since it's just a reference and don't need that much space as a real chess piece like new Rook() . 我感觉myChessBoard中的东西,比如myChessBoard [0] [0]不需要分配,因为它只是一个参考,并且不需要像new Rook()那样真正的棋子。

In short I feel I only need to allocate 32 chess pieces with new keyword (32 pieces on a chessboard) instead of 32 + 64 = 96 chess pieces. 总之,我觉得我只需要分配32个带有新关键字的棋子(棋盘上有32个棋子),而不是32 + 64 = 96个棋子。

I might have confused some basic concept in Java, please help. 我可能在Java中混淆​​了一些基本概念,请帮忙。

Edit: 编辑:

myChessBoard[][] = new ChessPiece[8][8] here I used new keyword to create 64 reference without real object. myChessBoard[][] = new ChessPiece[8][8]这里我使用new关键字创建64个没有真实对象的引用。 So new keyword doesn't necessarily create new objects, it can also create new references referring to null, right? 所以new关键字不一定会创建新对象,它也可以创建引用null的新引用,对吧?

that is wrong. 那是错的。 "the new keyword in that case is creating a new 2D array Object" thanks to accepted answer. “在这种情况下,新关键字正在创建一个新的2D数组对象”,这要归功于已接受的答案。

All you're doing with this public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; 所有你正在使用这个public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; is initializing a new game board. 正在初始化一个新的游戏板。 The game board has 64 squares (each square represented by a 2D index value), not 64 chess pieces. 游戏板有64个正方形(每个正方形由2D索引值表示),而不是64个棋子。 So each square can be given a chess piece value like myChessBoard[0][0] = new Rook(); 所以每个方块都可以给出像myChessBoard[0][0] = new Rook();

// or to give your pieces references
Knight blackKnight1 = new Knight();
myChessBoard[0][1] = blackKnight1;

Bishop blackBishop1 = new Bishop();
myChessBoard[0][2] = blackBishop1;

Queen blackQueen = new Queen();
myChessBoard[0][3] = blackQueen;

// and do the rest with all the other pieces according 
// to their position in the 2d array

When doing public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; you did not create a single ChessPiece . 你没有创建一个ChessPiece You only created an Array which is capable of having a reference to 64 ChessPieces. 您只创建了一个能够引用64个ChessPieces的Array。

All references will initially be null and you have to set them like myChessBoard[1][4] = new Rook() 所有引用最初都将为null ,您必须将它们设置为myChessBoard[1][4] = new Rook()

So there is no need to worry. 所以没必要担心。

I assume your ChessPiece class is abstract anyway, so it is impossible for Java to create a ChessPiece anyway. 我认为你的ChessPiece类无论如何都是抽象的,所以Java无论如何都不可能创建一个ChessPiece。

But one more point: Java has automatic garbage collection. 但还有一点:Java有自动垃圾收集。 So even in case you do create some unnecessary objects sometimes, the garbage collector will get rid of it pretty quickly. 因此,即使你有时会创建一些不必要的对象,垃圾收集器也会很快摆脱它。 Don't optimize prematurely. 不要过早优化。 Every computer nowadays can handle 96 or 32 objects and there won't be any noticable difference. 现在每台计算机都可以处理96或32个对象,并且没有任何明显的区别。

No. Writing new ChessPiece[8][8] does NOT allocate 64 ChessPiece objects. 编写new ChessPiece[8][8]不会分配64个ChessPiece对象。 It only creates an array, with 64 null references in it. 它只创建一个数组,其中包含64个空引用。 You need to make the ChessPiece objects separately, which is what you're doing. 你需要单独制作ChessPiece对象,这就是你正在做的事情。

Note that many OOP languages (in particular, C++) work differently in this respect. 请注意,许多OOP语言(特别是C ++)在这方面的工作方式不同。

public ChessPiece myChessBoard[][] = new ChessPiece[8][8] is an array allocation statement. public ChessPiece myChessBoard[][] = new ChessPiece[8][8]是一个数组分配语句。 It does not allocate 64 new ChessPiece objects, but a two dimensional array containing 8*8=64 references to ChessPiece s, most of which are null . 没有分配64个新的ChessPiece对象,而是一个二维数组,其中包含8 * 8 = 64个对ChessPiece的引用,其中大部分都是null Only wen populating each individual square do you actually create a new ChessPiece object. 只有填充每个单独的正方形才能实际创建一个新的ChessPiece对象。

Having said that, you do have a point, and this allocation policy does seem somewhat wasteful. 话虽如此,你确实有一点,这个分配政策看起来确实有些浪费。 My suggestion to you, since this is a learning project - hide the chessboard implementation in its own class (eg, ChessBoard ), which for now will contain your two dimensional array. 我给你的建议,因为这是一个学习项目 - 在自己的类中隐藏棋盘实现(例如, ChessBoard ),现在它将包含你的二维数组。 Once you've got everything else working to your satisfaction, you can go back and optimize the memory consumption of ChessBoard . 一旦你让其他一切工作满意,你就可以回过头来优化ChessBoard的内存消耗。

When you call ChessPiece myChessBoard[][] = new ChessPiece[8][8]; 当你打电话给ChessPiece myChessBoard[][] = new ChessPiece[8][8]; , you're not instantiating any ChessPiece objects. ,你没有实例化任何ChessPiece对象。 You're just creating a 2D array that can hold them. 您只是创建一个可以容纳它们的2D数组。 To give a real-life analogy, when you buy plastic containers to hold leftover food, they don't come with food in them- you put that into the containers later. 为了给出一个真实的类比,当你购买塑料容器来存放剩余的食物时,它们不会随附食物 - 你可以将它放入容器中。

When you create the arrays, you simply allocate the storage cells that start of as nulls, but can also be used to store chess pieces later, and you create 64 of these. 在创建数组时,您只需将以空值开头的存储单元分配,但也可以用于以后存储国际象棋,然后创建其中的64个。 No chess pieces so far. 到目前为止没有棋子。 Those still need to get created separately. 那些仍然需要单独创建。

Suppose you've built a prison with 10,000 cells. 假设你建造了一个拥有10,000个牢房的监狱。 That's great in itself, but you still have to find 10,000 prisoners to populate it. 这本身就很棒,但你仍然需要找到10,000名囚犯来填充它。

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

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