J
Jim T
I'm extremely new to java, so I'm sure this has been asked before.
Please bear with me. Also please pardon any typos in my pseudocode.
Coming from Objective-C land, I have a class hierarchy and a bunch of
methods that basically amount to this:
class SuperClass {
Vector loadFromDatabase {
SomeVariable myType = CLASS.myType();
//load stuff up in an interesting manner
//return my vector
}
static int myType {
System.out.println("OH NOES!");
}
}
class Subclass1 extends SuperClass {
static int myType {return "foo"; };
}
class Subclass2 extends SuperClass {
static int myType {return "bar"; };
}
class Subclass3 extends SuperClass {
static int myType {return "baz"; };
}
//inside some method
Vector stuff = Subclass1.loadFromDatabase();
Essentially, the routine to load from the database is identical for
all subclasses, the only thing that needs to vary is what type of
object is getting loaded, and that's defined as a class method in the
subclass. All works wonderfully well in Objective-C, but, of course, I
have access to a class variable there (in an instance method "self" is
the object, in a static method, "self" is the class itself), so it's
easy to do.
And I'm just at a loss for a java-ish way to do this. I could define
some sort of lookup table in the superclass, but that's silly - and
requires the superclass to know which subclasses it has. But I can't
figure out any other way to do it. Once I'm in the loadFromDatabase()
method, it always seems to call SuperClass's myType().
So how can I implement this cleanly?
While I'm at it, is there any way to dynamically choose a class to
call a class method upon? Again, in objective-C, you can do:
[[self class] someClassMethod];
So I'd think that the java equivalent would be:
this.getClass.someClassMethod();
But, alas, that doesn't seem to work either. Is this possible? And, if
so, how?
Many thanks,
-Jim...
Please bear with me. Also please pardon any typos in my pseudocode.
Coming from Objective-C land, I have a class hierarchy and a bunch of
methods that basically amount to this:
class SuperClass {
Vector loadFromDatabase {
SomeVariable myType = CLASS.myType();
//load stuff up in an interesting manner
//return my vector
}
static int myType {
System.out.println("OH NOES!");
}
}
class Subclass1 extends SuperClass {
static int myType {return "foo"; };
}
class Subclass2 extends SuperClass {
static int myType {return "bar"; };
}
class Subclass3 extends SuperClass {
static int myType {return "baz"; };
}
//inside some method
Vector stuff = Subclass1.loadFromDatabase();
Essentially, the routine to load from the database is identical for
all subclasses, the only thing that needs to vary is what type of
object is getting loaded, and that's defined as a class method in the
subclass. All works wonderfully well in Objective-C, but, of course, I
have access to a class variable there (in an instance method "self" is
the object, in a static method, "self" is the class itself), so it's
easy to do.
And I'm just at a loss for a java-ish way to do this. I could define
some sort of lookup table in the superclass, but that's silly - and
requires the superclass to know which subclasses it has. But I can't
figure out any other way to do it. Once I'm in the loadFromDatabase()
method, it always seems to call SuperClass's myType().
So how can I implement this cleanly?
While I'm at it, is there any way to dynamically choose a class to
call a class method upon? Again, in objective-C, you can do:
[[self class] someClassMethod];
So I'd think that the java equivalent would be:
this.getClass.someClassMethod();
But, alas, that doesn't seem to work either. Is this possible? And, if
so, how?
Many thanks,
-Jim...