Home > Back-end >  About Type interface and ParameterizedType interface between strong doubt ~!!
About Type interface and ParameterizedType interface between strong doubt ~!!

Time:09-21

, there is a doubt:

 public class GenricDaoImpl 
Implements GenricDao {

Private Class<?> EntityClass;//the type of entity class

Public GenricDaoImpl () {
EntityClass=(Class<? & gt;) ((ParameterizedType) (enclosing getClass ()
. GetGenericSuperclass ())). The getActualTypeArguments () [0];
}
}

 Type Type=this. GetClass () getGenericSuperclass (); 
ParameterizedType ParameterizedType=(ParameterizedType) type;

ParameterizedType interface is a Type of interface, Class Class Type is implementation Class;

But Class Class doesn't implement ParameterizedType interface, what is strong to no error;

Class Class didn't rewrite the getActualTypeArguments () method;

I wrote a similar since the Demo, called the com. Itany. C always be cast to com. Itany. B, the code is as follows:
 interface A {} 
Interface B extends A {}
Implements A class C {}
Public class Main {
Public static void main (String [] args) {
A, A=new C ().
B=B (B) a;
}
}

CodePudding user response:

First take a look at polymorphism,,,,

CodePudding user response:

reference 1st floor pany1209 response:
see first polymorphism,,,,


Boon? Polymorphism?

The key is no relationship between Class Class and ParameterizedType interface

CodePudding user response:

refer to the second floor iskaol response:
Quote: refer to 1st floor pany1209 response:

First take a look at polymorphism,,,,


Boon? Polymorphism?

The key is no relationship between Class Class and ParameterizedType interface

Type Type=this. GetClass (). GetGenericSuperclass ();
Type is the implementation Class ParameterizedType ParameterizedTypeImpl not Class...

CodePudding user response:

refer to the second floor iskaol response:
Quote: refer to 1st floor pany1209 response:

First take a look at polymorphism,,,,


Boon? Polymorphism?

The key is no relationship between Class Class and ParameterizedType interface

A and B interface is it doesn't matter, but with class A, class C also has relationship with B interface, your problem is the C class as A class A saw, so will only be entangled with the relationship between A and B, but it does not seem to be what you want to, you can get the method object is C type, although don't know what specific class, but should be inherited class A, implements the interface B

ABC yourself accordingly

CodePudding user response:

reference 4 floor maradona1984 response:
Quote: refer to the second floor iskaol response:

Quote: refer to 1st floor pany1209 response:

First take a look at polymorphism,,,,


Boon? Polymorphism?

The key is no relationship between Class Class and ParameterizedType interface

A and B interface is it doesn't matter, but with class A, class C also has relationship with B interface, your problem is the C class as A class A saw, so will only be entangled with the relationship between A and B, but it does not seem to be what you want to, you can get the method object is C type, although don't know what specific class, but should be inherited class A, implements the interface B

ABC yourself accordingly

Thank you! When first learning to see may not carefully

CodePudding user response:

A lot of things can not only see the surface, the return value is the implementation of the interface class, what the implementation class also implements the interface, you need to find out, not to return to the interface implementation class cannot achieve at the same time other interface

CodePudding user response:

Oh my god, the building Lord, you did not even understand the relational model of a class, or don't take into account the issue of generics,
Generics and inheritance are the two concepts,
Implements and extends is also a difference between,
The building Lord that error, not a generic problem, is the issue of succession,
You of the class C, no inheritance B interface, just inherited A interface, you will be forced into B C interface, affirmation is to be an error,
  • Related