找不到变量(继承问题)
好的,所以我遇到了问题。
我有两个类:ImageHandler 和 PixelHandler。
在我的 ImageHandler 类中,我声明:
public class ImageHandler{
private static BufferedImage myImage;
...
并且我尝试在 PixelHandler 中重新访问 myImage:
public class PixelHandler{
private int [] pix;
public int checkNumOfWindows(){
for(int x= 0; x < 1280; x++){
for(int y =0; y < 800; y++){
pix = myImage.getRGB(x, y, 6, 7, experimentalPattern, 0, 6);
}
}
}
...
当我尝试时,我收到错误消息:
找不到符号 - 变量 myImage
有什么建议吗?
Okay so I'm having a problem.
I have two classes: ImageHandler and PixelHandler.
In my ImageHandler class I declared:
public class ImageHandler{
private static BufferedImage myImage;
...
And I try to reacess myImage in PixelHandler:
public class PixelHandler{
private int [] pix;
public int checkNumOfWindows(){
for(int x= 0; x < 1280; x++){
for(int y =0; y < 800; y++){
pix = myImage.getRGB(x, y, 6, 7, experimentalPattern, 0, 6);
}
}
}
...
When I try to I get the error message:
Cannot find symbol - variable myImage
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的意思是扩展
ImageHandler
类吗?此外,派生类无法访问其父类的私有数据成员。如果您将变量声明为受保护,则子类将能够访问它:
Did you mean to extend the
ImageHandler
class?Also, derived classes cannot access private data members of their parents. If you declared your variable as protected, then child classes will be able to access it:
private
变量是类的私有变量(并且不能继承),这意味着您无法从另一个类访问它们。如果 PixelHandler 是图像处理程序的扩展,那么您应该将 myImage 声明为protected
,而不是private
。否则,您应该使用 ImageHandler.myImage 访问此变量,或者更好的是,在 ImageHandler 中声明一个静态方法来获取此变量。private
variables are private to the class (and also not inherited) which means you cannot access them from another class. If PixelHandler is an extension of Image handler, than you should declare myImage asprotected
, notprivate
. Else, you should acess this variable usingImageHandler.myImage
, or better, declare a static method in ImageHandler to get this variable.