Pattern, Abstract Factory / Factory
Luís Oliveira
luismbo at gmail.com
Sat Feb 6 19:55:16 UTC 2021
On Sat, 6 Feb 2021 at 16:29, Manfred Bergmann <manfred.bergmann at me.com> wrote:
> But this is kind of also possible in i.e. Java where you can say:
> Foo.class.newInstance() which uses reflection and is not normally used.
> But is effectively similar to make-instance `foo.
My Java is getting rustier and rustier, but maybe you can tell me why
the following program doesn't compile:
class Foo { }
class Bar extends Foo { }
class Baz extends Foo { }
class Main {
public static void main(String[] args) {
makeAndPrint(Foo.class);
makeAndPrint(Bar.class);
makeAndPrint(Baz.class);
}
public static void makeAndPrint(Class<Foo> klass) {
try {
Object o = klass.newInstance(klass);
System.out.println("Created: " + o.toString());
} catch (Exception e) {
System.err.println("Failed: " + klass);
}
}
}
The rough equivalent in Common Lisp would be:
(defclass foo () ())
(defclass bar (foo) ())
(defclass baz (foo) ())
(mapcar #'make-instance '(foo bar baz))
Luís
More information about the pro
mailing list