• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic and Card Deck  RSS feed

 
Giovanni Zotti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!
I wrote this code for an Italian Card Game:
------------------
Carta.java: // carta == card



Ok this is fine (I hope)... For now I don't care the compareTo method (needed to implement Comparable).
In an other class I could write the following static/class variable:



And, in the main method:



But it isn't funny enough.
So I tried to write a deck class: Mazzo.java:



Now I'd like to use generics, and here the Compiler complains!



The parameter - type <Carta> hiddens my Carta class...
So I tried to use wildcards:

[Added code tags]

Now all works (or at least: compiles!).
But... WHY?
Why it didn't compile with "class Mazzo <Carta> extends LinkedList<Carta>???
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, welcome to the Ranch. Please can you use the [code] tags for your properly indented source code. It's very hard to read as it is now and you'll attract much better responses if we can read your code easily.

Also, you mention a compilation error. What exactly was the error? Paste in the relevant part of the compiler output.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Giovanni. Welcome to the Ranch!

I've added code tags to make your post easier to read - see UseCodeTags for instructions on how to do this yourself.

When you write:
class Mazzo <Carta> extends LinkedList<Carta>
This is identical to writing:
class Mazzo <T> extends LinkedList<T>
In other words, you are declaring Mazzo as a generic class, with its own type parameter which you've called Carta. This is entirely separate from the real class Carta. Using existing class names as a type parameter is entirely legal (otherwise you could break all sorts of things by defining a T class), but it's a bad idea because it causes exactly the sort of confusion you're experiencing.

So when you write:
for (Carta.Seme s : Carta.Seme.values())
inside this class it's using the type parameter Carta, not the class Carta. But the type parameter can be anything, so it doesn't have Seme or Valore.

Your fix compiles because you've removed the ambiguity. But I think what you really want is even simpler. Notice that you don't use E anywhere else. You don't need a generic Mazzo class, you just want it to use the generic features of LinkedList. So what I'd actually suggest is:




 
Giovanni Zotti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excuse me for my carelessness.




Errors:
2x
Illegal qualified access from the type parameter Carta (line 6; line 7)
Seme cannot be resolved or is not a field (line 6)
Valore cannot be resolved or is not a field (line 7)

With:

all compiles and runs :-)
thank you!





 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!