[fpc-pascal] Java equivalent of Pascal's class reference functionality
Graeme Geldenhuys
mailinglists at geldenhuys.co.uk
Thu Jun 9 02:36:26 CEST 2016
On 2016-06-09 00:46, Maciej Izak wrote:
> ± yes, it is called Class and you need no-arg constructor for that:
Thank you very much Maciej. I couldn't get your code to work. Eclipse
(thus Java Compiler) insisted that I replace Class<TVisitor> with
Class<TShowNameVisitor> which completely defeats the exercise - the
whole point is that I don't know which TVisitor descendant class will be
pass in. Very weird.
But, your information got me on the right track. I read some docs, did
some more searching and came up with the following solution which works.
Step 1 seems complete. :)
public class ClassReferenceTest {
public void run() {
System.out.println("Calling executeVisitor() from ClassReferenceTest...");
executeVisitor(this, TShowNameVisitor.class);
executeVisitor(this, TShowEmailVisitor.class);
}
public void executeVisitor(ClassReferenceTest pData, Class<? extends
TVisitor> pVisClass) {
TVisitor lVisitor = null;
try {
lVisitor = pVisClass.newInstance();
lVisitor.execute(this);
} catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ClassReferenceTest t = new ClassReferenceTest();
t.run();
}
}
Console Output:
================
Calling executeVisitor() from ClassReferenceTest...
ShowNameVisitor is executing against ClassReferenceTest
ShowEmailVisitor is executing against ClassReferenceTest
Perfect!
Regards,
Graeme
More information about the fpc-pascal
mailing list