[英]Access one class from another
I have a class defined which publishes a method allowing access to a private object within: 我定义了一个类,该类发布了一种允许访问内部私有对象的方法:
public class HexBoard {
[...]
public HexBoard(int Width, int Height, boolean Wrap) {
SetSize(Width, Height); // inherently calls Reset()
SetWrap(Wrap);
} // HexBoard constructor
public Polygon GetHexagon(int CellIndex) {
Polygon p = new Polygon();
for (int i = 0; i < 6; i++) {
p.addPoint((int) (HexCentres.X(CellIndex) + HexPoints.X(i)), (int) (HexCentres.Y(CellIndex) + HexPoints.Y(i)));
}
return p;
} // GetHexagon
public int Cells() { return CellCount; }
} // HexBoard
You can see that the method creates a polygon and returns it. 您可以看到该方法创建了一个多边形并将其返回。 This bit works well. 这一点效果很好。 Now, I have another class, which publishes an extended JPanel in order to draw a hexagon-based playfield consisting of lots of hexagons. 现在,我还有另一个类,该类发布了扩展的JPanel,以便绘制包含许多六边形的基于六边形的运动场。
import java.awt.*;
import javax.swing.*;
public class PCinterface extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int CellCount = HexBoard.Cells();
for (int i = 0; i < HexBoard.Cells(); i++) {
g.drawPolygon(HexBoard.GetHexagon(i));
}
} // paintBoard
} // PCinterface
The problem is that PCinterface knows nothing of HexBoard, so it can't access HexBoard.Cells() or HexBoard.GetHexagon(). 问题在于PCinterface不了解HexBoard,因此无法访问HexBoard.Cells()或HexBoard.GetHexagon()。
When the following is executed 当执行以下命令时
public class Test {
public static void main(String args[]) {
BreadBoard Board = new BreadBoard(12,12,false);
Board.SetCellRadius(25);
JFrame frame = new JFrame();
frame.setTitle("BreadBoard");
frame.setSize(600, 600);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(new PCinterface());
frame.setVisible(true);
*/
} // main
} // Test
I would hope it would open a window and draw some hexagons, but I can see that the hexagon based board created in main using HexBoard, doesn't exist in the context of PCinterface. 我希望它会打开一个窗口并绘制一些六边形,但是我可以看到在PCinterface上下文中不存在主要使用HexBoard创建的基于六边形的板。
I can see tat I could readily include PCInterface in main and that would solve the problem: I'm trying to develop for multiple platforms and had hoped this was an appropriate way to separate platform dependant classes. 我可以看到我可以很容易地将PCInterface包含在main中,这将解决问题:我正在尝试为多个平台进行开发,并希望这是分离依赖于平台的类的适当方法。
How can I employ the data held in BreadBoard within the PCInterface class, please? 请问如何使用PCInterface类中的BreadBoard中保存的数据?
You need an instance of a HexBoard. 您需要一个HexBoard实例。 you might add one to you your PCinterface 您可以在PC界面中添加一个
public class PCinterface extends JPanel {
public HexBoard hexBoard
public PCinterface(HexBoard board)
{
this.hexBoard = board;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int CellCount = this.hexBoard.Cells();
for (int i = 0; i < this.hexBoard.Cells(); i++) {
g.drawPolygon(this.hexBoard.GetHexagon(i));
}
} // paintBoard
} // PCinterface
assuming that the type of Board
, BreadBoard
extends HexBoard
, you can pass it into the constructor like this 假设Board
的类型, BreadBoard
扩展了HexBoard
,您可以像这样将其传递给构造函数
contentPane.add(new PCinterface(Board));
As @HunterMcMillen commented, you need to instantiate HexBoard
in order to use the method Cells()
: 正如@HunterMcMillen所评论的那样,您需要实例化HexBoard
才能使用Cells()
方法:
...
HexBoard hexBoard = new HexBoard(width, height, wrap);
int cellCount = hexBoard.Cells();
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.