現在、スーパークラスをパラメーターとして使用して関数を実装しています。
例えば:
private void foo(Parent parent) {
if(parent.getClass() == Child1.class) {
Child1 c1 = (Child1) parent;
System.out.println(c1.getChild1Attribute());
}
else if(parent.getClass() == Child2.class) {
Child2 c2 = (Child2) parent;
System.out.println(c1.getChild2Attribute());
}
else if(parent.getClass() == Parent.class) {
System.out.println(parent.getParentAttribute());
}
}
これは悪い考えですか?
getClass()
ここでorを使用するのinstanceof
は悪いデザインだと言っているスレッドをいくつか読みました: