简体   繁体   中英

calling specific methods of generic types in ny equals() method

I am working on the equals() method for my sparse matrix class that I'm writing (part of a school project). I am constantly runnung into the issue that it won't let me use any methods or other members specific to my class, because that (the name I used for the parameter to equals ) has to be of the generic type Object in order to override Objects 's equals() method. Even beyond that, I need to be able to use some of the methods of my SparseMatrix's type parameter in order to really compare the equality, unless I can also figure ou. How can I write it to get around that obstacle?

I have a few ideas how I night do it, but none of them seem to work: I've triedcasting the parameter, I've tried overlading equals() , I've even tried some other stuff, but none of it seems to work.

This is what I have so far, but, as I said, I just can't get it to work.

public boolean equals(Object that) {
    if (that instanceof SparseMatrix<?>) { 
        if (this.xSize != that.xSize ||
                this.ySize != that.ySize)
            return false;
        /* make some more comparisons that depend on specific
         * members of my matrix class, etc...*/
    }
    return false;
}

I have tried searching SO for this, and while I was able to find a few that seemed to be asking the same thing, I couldn't find any answers that actually explained how to do it.

When you have an object of a base class and you know which kind of subclass it is, you can convert it by downcasting it. Then you can call any methods specific to the subclass.

public boolean equals(Object o) {
    if (o instanceof SparseMatrix<?>) { 
        SparseMatrix that = (SparseMatrix)o;  // <-- downcast
        if (this.xSize != that.xSize ||
                this.ySize != that.ySize)
            return false;
    }
    return false;
}

Should o not be an instance of SparseMatrix or a class which extends/implements SparseMatrix (you already checked that, but let's assume you didn't) you would get a ClassCastException.

public boolean equals(Object that) {
    if (that !=null && that instanceof SparseMatrix<?>) { 
        SparseMatrix that = (SparshMatrix)o;
        if (this.xSize != that.xSize ||
                this.ySize != that.ySize)
            return false;
    }
    return false;
}

Added Code to check for null value.

You need to have this method overrided in the class whose instances you want to compare .. And when you actually invoke equals on instances of that class, this overrided method will be invoked..

** Moved from comment: - Using instanceof you can only assure of what type your instance is, and not the parameterized type of that instance.. (You cannot findout whether you have ArrayList<String> , what you can find out is that it is ArrayList or ArrayList<?> to be precise) .. Reason for that is, generic type information is only available at compile time , and is erased through type erasure , so they are not available at runtime..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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