Java – why doesn’t class have a good generic type in this case?
•
Java
In this code, why can't you declare a type as class? Extension b >
public class Foo<B> { public void doSomething(B argument) { Class<? extends Object> type = argument.getClass(); } }
Solution
The problem is that Java syntax does not allow getClass () to indicate that it returns a type that matches its defined class, and this is not a special case for the compiler So you were forced to surrender
In many cases, you want to be able to specify this type, for example, for links, so I want them to include this feature one day
You can write
Class<? extends this> getClass();
or
public this clone(); // must return a type of this class.
or
class ByteBuffer { this order(ByteOrder order); } class MappedByteBuffer extends ByteBuffer { } // currently this won't work as ByteBuffer defines order() MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE,fc.size()) .order(ByteOrder.nativeOrder());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码