问题
I am having trouble understanding with some of the code snippets about this part of the Java tutorial: http://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html
public Object findLargest(Object object1, Object object2) {
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ((obj1).isLargerThan(obj2) > 0)
return object1;
else
return object2;
}
and:
public interface Relatable {
// this (object calling isLargerThan)
// and other must be instances of
// the same class returns 1, 0, -1
// if this is greater than,
// equal to, or less than other
public int isLargerThan(Relatable other);
}
- In the first example, why am I downcasting Object types into Relatable types? What happens if the first method doesn't include the first two statements?
- Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?
回答1:
You cast the
ObjectsintoRelatabletypes because otherwise you cannot use the methods declared in theRelatableinterface. SinceObjectdoes not have theisLargerThanmethod, you would get a compiler error without casting.
Honestly, in my opinion thefindLargestmethod as shown here was not very well designed; a better illustration of the purpose of Interfaces would be to ask forRelatableobjects as the parameters like so:public Object findLargest(Relatable object1, Relatable object2) { //implementation not shown to save space }
This way, the user must passRelatableobjects, but they can pass any object whose class implementsRelatable(such asRectangle)"If I know that I'm comparing two Rectangle objects..."
True, if you know that you are comparing two Rectangle objects, there is little use for an interface, but the purpose of interfaces is to allow you to create a generic "type" of object that can be used to define common features of several different classes.
For example, what if you also had aCircleclass and aSquareclass (both of which implementedRelatable)? In this case, you do not necessarily know the exact type of object you have, but you would know that it isRelatable, so it would be best to cast to typeRelatableand use theisLargerThanmethod in a case like this.
回答2:
Interfaces define a set of methods which every class which the interface implements has to implement. The downcast is necessary to get access to these methods.
You don't know if you are comparing rectangles with this interface. You could get any Relatble passed. This is one of the cases generics come in handy.
回答3:
1.In the first example, why am I down casting Object types into Relatable types? What happens if the first method doesn't include the first two statements?
Answer
Every object has some basic functionality and you want a specific object write now. You are down casting your object into a "Relatable" so you can use the "isLargerThan" method(an object wont have it since it has only basic common stuff).
If you didn't down cast, you would not pass compilation.
2.Let's say I wrote a Rectangle class that implements the Relatable interface and has the "findLargest" method. If I know that I'm comparing two Rectangle objects, why not just make the first method downcast the objects into Rectangles instead?
Answer
Since you want to create something generic.
Lets say you have a Student and a Driver. Both of them are People. You can create an interface called IPeople and make both the Student and the driver implement it.
IPeople will have a method called "getAge()" that each of them will implement.
IPeople will have all the functionality that you need for "People". That's how you create cross object functionality under the "same hat".
来源:https://stackoverflow.com/questions/24968485/java-help-understanding-the-use-of-interfaces-as-a-data-type