たとえば、説明するクラスがある場合。Person という名前の people と、Child、Adult Man、Adult Woman などの 6 つのサブクラスがあります。それらはすべて ID、髪の色、目の色などを持っています。異なるのは外見だけなので、すべてのサブクラスにはその独自の paint() メソッド。すべての人は、プログラムがフレーム上のどこに描画する必要があるかを示す 2 つの座標を持っています。すべてのサブクラスは、次のようにそれらの座標を取得します。
class AdultMan extends Person
{
AdultMan(int x, int y) {
super(x,y);
// I haven't yet worked with the hair color, eye color...
// only the coordinates to test my idea out
}
public void paint(Graphics g) {
int x = getX();
int y = getY();
// The drawing of an adult man from basic shapes
// based on the given coordinates (and colors later)
}
}
したがって、他のクラスでは、指定されたデータを処理し、それらすべてをMap<Integer,Person>
(Integer は ID) のようなマップに配置し、Jframe を拡張するクラスでは、マップの値をコレクションに配置し、次のように反復処理します。
for (Person person : persons)
{
// (persons is the name of my collection)
if(person.typeName.equals("adultMan"))
{
person = new AdultMan(person.x,person.y);
person.paint(g);
}
}
6タイプの人がいるので、どのタイプでもやりたいです。問題は、私のマップに最大 40 人がいる場合、30 人の大人の男性がいる可能性があり、これはフレームの最初の 1 つだけを描画し、次の異なるタイプにスキップすることです。