How do I use an extended generic type's type in a class?

manixrock

Say I have some classes as follows:

class A { }

class B<TA extends A> { }

class C<TB extends B<? extends A>> {
    TA varOfTypeTA; // this throws an error obviously
}

How can I define the type of varOfTypeTA to be whatever TB's TA is?

I know I can define it as A varOfTypeTA, but I want it to be of type TA not A.

I tried forcing it to define TA with class C<TB extends B<TA extends A>> so I can use TA, but this throws a syntax error.

To clarify, if I then define these classes:

class Horse extends A { }

class HorseHerd extends B<Horse> { }

class HorseHerder extends C<HorseHerd> {
    Horse getFavoriteHorse() { return varOfTypeTA; } // varOfTypeTA defined in class C must be of type Horse, otherwise I have to cast
}

How can I make varOfTypeTA be of type Horse?

Erick Robertson

Also declare TA as a generic in the same class.

public class C<TA extends A, TB extends B<TA>> {
    TA varOfTypeTA;
}

Then you can use TA in your class.

class HorseHerder extends C<Horse, HorseHerd> {
    Horse getFavoriteHorse() { return this.varOfTypeTA; }
}

나는 또한 선언의 순서가 여기서 중요하지 않다는 것을 알고 놀랐습니다. TA먼저 선언하고 싶지만 이 코드도 다음과 같이 컴파일됩니다.

public class C<TB extends B<TA>, TA extends A> {
    TA varOfTypeTA;
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

From an Interface how do I return a generic/dynamic type from a function

분류에서Dev

Cast to a type from a generic class

분류에서Dev

How to run method that returns an object of "Type" from generic class

분류에서Dev

How to run method that returns an object of "Type" from generic class

분류에서Dev

What is "Type mismatch" and how do I fix it?

분류에서Dev

Generic class with explicitly type-safe taxonomy

분류에서Dev

How do I use the GeometryConstraint class?

분류에서Dev

How to set a constraint for a type so it must be of another generic typed type

분류에서Dev

How can I use True Type Collection (.ttc) in JavaFX?

분류에서Dev

how can i determine the ACTUAL return type of a specific subclass of a generic superclass?

분류에서Dev

How do I Alter an attribute to change its datatype in Object type?

분류에서Dev

How do I update a plot type with RadioGroup buttons?

분류에서Dev

How do I know what type of document handleOpenURL is responding to?

분류에서Dev

How do I type the tick and backtick characters on Windows?

분류에서Dev

Hibernate, how do I get the custom type to register

분류에서Dev

How do I get the image url from an image field type

분류에서Dev

How do I force Excel to always type purple text?

분류에서Dev

How do I fold an MPL type list into a variadic container?

분류에서Dev

How can I determine a variable's type in a vim function?

분류에서Dev

Spring AOP - using joinpoint to get generic class type (JAVA)

분류에서Dev

Java: Is it possible to compare a Class<?> object with a generic type parameter?

분류에서Dev

How can I get the type of a Type?

분류에서Dev

Using a bounded generic class to extend another generic class produces an unknown type

분류에서Dev

Generic type conversion in generic method

분류에서Dev

How do I use a HashMap from another class or function?

분류에서Dev

why do I have to declare an irrelevant struct file_handle variable before I can use that type?

분류에서Dev

Union type for generic param

분류에서Dev

Java Generic type identification

분류에서Dev

generic data type id

Related 관련 기사

  1. 1

    From an Interface how do I return a generic/dynamic type from a function

  2. 2

    Cast to a type from a generic class

  3. 3

    How to run method that returns an object of "Type" from generic class

  4. 4

    How to run method that returns an object of "Type" from generic class

  5. 5

    What is "Type mismatch" and how do I fix it?

  6. 6

    Generic class with explicitly type-safe taxonomy

  7. 7

    How do I use the GeometryConstraint class?

  8. 8

    How to set a constraint for a type so it must be of another generic typed type

  9. 9

    How can I use True Type Collection (.ttc) in JavaFX?

  10. 10

    how can i determine the ACTUAL return type of a specific subclass of a generic superclass?

  11. 11

    How do I Alter an attribute to change its datatype in Object type?

  12. 12

    How do I update a plot type with RadioGroup buttons?

  13. 13

    How do I know what type of document handleOpenURL is responding to?

  14. 14

    How do I type the tick and backtick characters on Windows?

  15. 15

    Hibernate, how do I get the custom type to register

  16. 16

    How do I get the image url from an image field type

  17. 17

    How do I force Excel to always type purple text?

  18. 18

    How do I fold an MPL type list into a variadic container?

  19. 19

    How can I determine a variable's type in a vim function?

  20. 20

    Spring AOP - using joinpoint to get generic class type (JAVA)

  21. 21

    Java: Is it possible to compare a Class<?> object with a generic type parameter?

  22. 22

    How can I get the type of a Type?

  23. 23

    Using a bounded generic class to extend another generic class produces an unknown type

  24. 24

    Generic type conversion in generic method

  25. 25

    How do I use a HashMap from another class or function?

  26. 26

    why do I have to declare an irrelevant struct file_handle variable before I can use that type?

  27. 27

    Union type for generic param

  28. 28

    Java Generic type identification

  29. 29

    generic data type id

뜨겁다태그

보관