简体   繁体   English

我可以在每个对象上使用contains方法吗?

[英]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类,在另一个类中,我创建了两个名为cookiemaccaroni实例。 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所指出的那样,你的字段pxxyy应该是private ,绝对不是 static

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

相关问题 如何在每种方法中使用对象数组? - How can I use my object array at every method? 我可以在Java中将对象用作方法的变量吗? - Can I use an object as a variable of a method in java? contains()方法应使用哪些参数? - What parameters should I use for the contains() method? 我可以在 Arraylist.contains 方法的参数中查询对象的某个属性吗? - Can I query a certain attribute of an Object right inside the parameter of the Arraylist.contains method? 如何使用.contains方法检查整数值是否在二维数组中? - How can I use .contains method to check if integer value is in an two dimensional array? 如何将对象中的 Arraylist 用于另一个类中的方法 - How can I use an Arraylist in an object for a method in another class 如果对象由多维数组保存,可以使用该方法吗? - Can I use the method if the object is held by an multidimensional array? 是否可以将同一个对象及其值用作其他对象? - Can I get the same object with its vales and use it in a different method? List包含对象的方法 - List contains method for object 如何在Spinner对象中使用自定义Spinner类每次触发OnItemSelectedListener事件 - How can I use custom Spinner class in spinner object to fire OnItemSelectedListener event every-time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM