| |
"Dynamic Tools: Evolutive Java Applications"
Vol. 7, Issue 1, p. 70
Listing 1: Definitions used in the example
class Person
{
public Person (String n, int a)
{
name=n; age=a; allPersons.add(this);
};
public static Enumeration getAll()
{
return allPersons.elements();
};
public String getName()
{
return name;
} ;
protected static Vector allPersons =
new Vector();
protected String name ;
protected int age ;
}
public class Man extends Person
{
public Man (String n, int a) {super(n, a);}
}
public class Woman extends Person
{
public Woman (String n, int a) {super(n, a);}
}
public interface Print
{
void toStandardOutput() ;
}
Listing 2: Full solution public class
PrintAllPersons
{
public static void main (String [] arg)
{
Enumeration e = Person.getAll();
while (e.hasMoreElements())
{
Print d = (Print) e.nextElement() ;
d.toStandardOutput () ;
}
}
}
abstract class DA_Person
{
public int age;
}
public abstract class DI_Person__Print
implements Print
{
public void toStandardOutput()
{
Person p = (Person) (Object) this ;
System.out.println (p.getName()) ;
DA_Person pp = (DA_Person) (Object) this ;
System.out.println (pp.age) ;
}
}
|
|