1
我有一个对象说有不同的方法获取不同汽车制造商的汽车例如奥迪,宝马,Merc等。所有这些车型都没有任何通用接口或抽象类,但是具有车轮,刹车等一些共同特性。汽车对象有一种方法来识别应使用哪种类型的制造商对象来提取属性。任何人都可以建议我从汽车物体中提取车轮的好方法吗?如何从java中的不同类别的对象获取公共属性
public class Car {
public Audi getAudi() { return this.audi; }
public BMW getBMW() { return this.bmw; }
public Merc getMerc() { return this.merc;)
public String getMaker() { return this.maker; }
}
public class Audi {
public Wheel getWheel() { return this.wheel; }
public Brakes getBrakes() { return this.brakes; }
}
public class BMW {
public Wheel getWheel() { return this.wheel; }
public Brakes getBrakes() { return this.brakes; }
}
public class Merc{
public Wheel getWheel() { return this.wheel; }
public Brakes getBrakes() { return this.brakes; }
}
请添加一些代码,因为它现在*一个选项*是使用反射,但我建议尽可能避免它。 – skiwi
好吧,你可能想要检查(正式)无关的类。检查这个线程:http://stackoverflow.com/questions/22200676/recursively-list-all-properties-exposed-by-a-class/22201894#22201894 – robermann
“所有这些汽车类没有任何通用的接口或抽象类在他们身上“为什么会有人想要建立一个系统的方式? –