s***8 发帖数: 1136 | 1 I have the following generic interface:
public interface GenericIntf {
public T hello(T);
}
and an impl class:
public class HelloImpl implements GenericIntf {
public Date hello(Date d) {
return d;
}
}
When I reflect on HelloImpl class to get all methods with HelloImpl.class.
getMethods(), I got 2 methods, instead of 1:
public Date hello(Date);
public Object hello(Object)
I was expecting to see the first one (the one that has resolved to the
actual Date type). Is this expected | q*********u 发帖数: 280 | 2 看看doc,
getMethods
public Method[] getMethods()
throws SecurityException
Returns an array containing Method objects reflecting all the public member
methods of the class or interface represented by this Class object,
including those declared by the class or interface and those inherited from
superclasses and superinterfaces. Array classes return all the (public)
member methods inherited from the Object class. The elements in the array
returned are not sorted and are not in any
【在 s***8 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I have the following generic interface: : public interface GenericIntf { : public T hello(T); : } : and an impl class: : public class HelloImpl implements GenericIntf { : public Date hello(Date d) { : return d; : } : }
| s***8 发帖数: 1136 | 3 Thanks!
I've omitted methods inherited from Object. The part I don't understand is,
why hello method appears twice. Aren't they the same method, since
HelloImpl just implements the same method declared in GenericIntf?
If I don't use any generics, let HelloImpl implement Runnable interface, the
run() mehtod only appear once when reflecting on HelloImpl. | q*********u 发帖数: 280 | 4 I believe T yourMethod() and String yourMethod() are diferent.
According to 'Thinking in Java', I remember before 1.5, java use generic '
Object' as 'T';
is,
the
【在 s***8 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Thanks! : I've omitted methods inherited from Object. The part I don't understand is, : why hello method appears twice. Aren't they the same method, since : HelloImpl just implements the same method declared in GenericIntf? : If I don't use any generics, let HelloImpl implement Runnable interface, the : run() mehtod only appear once when reflecting on HelloImpl.
| s******n 发帖数: 876 | |
|