I have a question about generic, then say that I have 3 squares "foo", "bar" (which fu Subclass is), a square "factory" and an interface "test interface". I want to write a general method, which only allows objects that implement the "straight" interface, so there is no subclass. This is very difficult for me, because Java 8 seems "smart" compared to Java 7.
Then there is an example:
Public Interface Test Interface & lt; Expansion of T-Test Interface & lt; T & gt; & Gt; Public Square Foo Exam Interface & lt; Foo & gt; {}
Expands the public class bar {/ code>
Fu {}
Public class factory {Public stable and lieutenant; Expands T's Test Interface & lt; T & gt; & Gt; T doSth (T-ARGE) {return arg; }}
OK, these are my classes. I now want to allow Foo-objects in this case because of the doSth (T arg)
method This is the only class that implements the "straight" interface.
Obviously sth will no longer compile in this way:
b> factory.dos (new bar); Since the bar TestInterface & lt; Bars & gt;
OK, my problem is now, that this line will compile without any issues:
FUF = FACTORY. Doth (new bar);
Although the normal parameters should also apply the interface to T and T with their own class (sorry, actually do not know how to say it, hope you know it That's what I mean) The compiler accepts "bar" as the parameter
then the compiler should "behave like okay, the bar does not fit, but maybe a super class would fit." And then it "sees" that it works with Superlex "Foo". And then the bar will be explained like fu because every time there is some type of fu or something like that ?? At least this is how I imagine it.
OK, if I try to compile the line with Java 7, then it fails. So it seems that the Java 8 is a bit smarter with type-arasher ?!
There is no way to allow subclasses in Java 9, so that it will not be compiled ?:
Foo F = Factory.doth (new bar) );
Hope you will understand my problem, I'm rarely writing in English.
Greetings
Reasons Foo F = Factory.dom (new bar ());
Works in Java 8 and Java is not 7 because Java 8 has a better type of estimate in Java 7 this type is estimated:
FUF = Factory. & Lt; Bars & gt; Dasath (new bar ()); Bar
TestInterface , but this is TestInterface & lt; Foo & gt;
is applied by inheritance to the Java 8 type of estimation, and this type would be estimated:
FUF = Factory & Lt; Foo & gt; Dasath (new bar ());
Note that factory. & Lt; Foo & gt; DoSth ()
expects that Foo
as argument is applied to new bar ()
implications on foo
that works fine Does.
I am afraid that there is no way around it. You will always be able to uplift an object in your superclass. You can create Foo
last
, but it does not allow full subclassing of Foo
.
No comments:
Post a Comment