[英]Can I use the contains method on every object?
Can the contains
method be used on instances of classes I've written myself? 可以在我自己编写的类的实例上使用contains
方法吗?
I've created a class named Ana
and in another class, I've created two instance of it called cookie
and maccaroni
. 我创建了一个名为Ana
类,在另一个类中,我创建了两个名为cookie
和maccaroni
实例。 This is one of them: 这是其中之一:
Ana cookie = new Ana(cc.c, 20, 450);
At some point I am trying to invoke 在某些时候,我试图调用
if (maccaroni.contains(cookie.image(), cookie.xi(), cookie.yi())
{
return true;
}
but Netbeans underlined it to indicate a mistake. 但Netbeans强调它表明存在错误。 When I hovered over it, it said: 当我盘旋它时,它说:
no suitable method found for contains(Image,int,int) method Component.contains(int,int) is not applicable (actual and formal argument lists differ in length) method Component.contains(Point) is not applicable (actual and formal argument lists differ in length) method JComponent.contains(int,int) is not applicable (actual and formal argument lists differ in length)
Why the following code is regarded as a mistake by Netbeans? 为什么以下代码被Netbeans视为错误?
This is the class I'm referring to. 这是我所指的课程。
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JComponent;
public class Ana extends JComponent
{
public static Image p;
public static int xx;
public static int yy;
public Ana(Image io, int x, int y)
{
p = io;
xx = x;
yy = y;
repaint();
}
@Override
public void paint(Graphics g)
{
g.drawImage(p, xx, yy, null);
}
public Image image()
{
return p;
}
public int xi()
{
return xx;
}
public int yi()
{
return yy;
}
}
There is no contains()
method defined in Ana
. Ana
没有定义contains()
方法。 There is a contains()
method defined in JComponent
. 在JComponent
定义了一个contains()
方法。 It takes two integers. 它需要两个整数。 You're trying to invoke it with an image and two integers. 你试图用图像和两个整数来调用它。 You can probably get what you need by writing it like this: 您可以通过这样编写它来获得所需内容:
if (maccaroni.contains(cookie.xi(), cookie.yi())
{
return true;
}
So the kind of error in this code is: it's a compile error. 所以这段代码中的错误是:这是一个编译错误。 NetBeans isn't just underlining it, there is a message somewhere on your screen telling you what is wrong. NetBeans不只是强调它,屏幕上的某个地方会显示一条消息,告诉您哪里出了问题。 You may have to mouse over the underline or look in a different section of the screen--I don't use NetBeans on a regular basis so I don't recall exactly--but I assure you the message is there. 您可能必须将鼠标悬停在下划线上或查看屏幕的其他部分 - 我不会定期使用NetBeans,所以我不记得确切 - 但我向您保证信息就在那里。
And no, you can't invoke contains()
on any object, only on objects which classes define a contains()
method (with proper parameters), or that inherit such a method from one of their supertypes (from an ancestor class or interface they implement). 不,你不能在任何对象上调用contains()
,只能在类定义contains()
方法(具有适当参数)的对象上调用,或者从一个超类型(从祖先类或接口)继承这样的方法他们实施)。
Methods which all objects could use are those inherited from java.lang.Object
class, since all classes explicitly or implicitly extend it (parent of parent of parent... finally will need to extend the Object
class). 所有对象都可以使用的方法是从java.lang.Object
类继承的方法,因为所有类都显式或隐式地扩展它(父类的父类...最终将需要扩展Object
类)。
But there is no contains(Image, int, int)
method defined on java.lang.Object
. 但是在java.lang.Object
上没有定义contains(Image, int, int)
方法。 The only contains()
method available to instances of the Ana
class is the one inherited from JComponent
which is contains(int, int)
. Ana
类的实例唯一可用的contains()
方法是从JComponent
继承的contains(int, int)
。 As you see it accepts two int
arguments, which prevents you from using it with the additional Image
argument. 如您所见,它接受两个int
参数,这会阻止您将其与其他Image
参数一起使用。
By the way, as @PShemo has noted, your fields p
, xx
and yy
should likely be private
and most definitely not static
. 顺便说一下,正如@PShemo所指出的那样,你的字段p
, xx
和yy
应该是private
,绝对不是 static
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.