Re: abstract classes and generic types
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
---910079544-1362332109-1242652566=:7475
Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8BIT
On Sun, 17 May 2009, horos11@gmail.com wrote:
On May 17, 3:29?pm, Mark Space <marksp...@sbc.global.net> wrote:
I think I came up with the same thing Giovanni did, except "put" needs
to return a String to match your example.
horo...@gmail.com wrote:
abstract class a<U>> {
? ? String calculate() { return this.put(this.my_func()); }
? ? ? ?abstract String put( U u );
? ? ? ?abstract U my_func();
}
Otherwise use this the same way as his example.
Note: ?you can' parameterize with primitives. ?If you want to use float
or int, gotta write those by hand yourself. ?Sorry.
Ok, I guess I'll morph this issue a bit. I came to the conclusion that
generics were the way to go, but why should I need to define a parameter
with a class to do what I want to do?
This has been discussed here many times before. The upshot is that because
of the way java's generics work, you can't get away from defining the
parameter as part of the exposed interface. You require clients to write:
HorosFunkyClass<?>
Instead of just:
HorosFunkyClass
When dealing with it polymorphically. This is a wart, but a small one.
However ...
I have thought of a way you could do it without the wart, using
deviousness.
The trick is to push the variable into another, private, class, and then
refer to it via a <?>-bound variable. You can then manipulate that object
via a private generic method, which provides a local type-bound context to
do the work in. I should warn you that this is third-level generics
voodoo.
Here we go:
abstract class FunkyClass {
private FunkyHelper<?> helper;
protected FunkyClass(FunkyHelper<?> helper) {
this.helper = helper;
}
public void calculate() {
doCalculate(helper);
}
private <T> void doCalculate(FunkyHelper<T> helper) {
helper.things.add(helper.myFunc());
}
}
class IntegerFunky extends FunkyClass {
public IntegerFunky() {
super(new IntegerFunkyHelper());
}
}
class FloatFunky extends FunkyClass {
public FloatFunky() {
super(new FloatFunkyHelper());
}
}
abstract class FunkyHelper<T> {
public Set<T> things;
public abstract T myFunc();
}
class IntegerFunkyHelper extends FunkyHelper<Integer> {
public Integer myFunc() {
return 1;
}
}
class FloatFunkyHelper extends FunkyHelper<Float> {
public Float myFunc() {
return 1.0f;
}
}
Study and be enlightened.
tom
--
I am become Life, destroyer of worlds
---910079544-1362332109-1242652566=:7475--