objectA gives objectB to objectC

From: thufir (thufir.hawat_at_mail.com)
Date: 01/27/05


Date: 27 Jan 2005 00:43:26 -0800

given that the class Sword is in an entirely different package,
Weapons.jar, for which the source code is unavailable, can one monster
give an instance of Sword to a second monster instance? something
like:

{ //put this in TestDriveMOnsters
Sword excalibur = new Sword();
Monster barney = new Monster();
Monster fred = new Monster();
//barney gets excalibur, but how?
//barney gives excalibur to fred, but how?
}//end example

How does barney even get excalibur?

//////////////code//////////////
package atreides.monsters;

public class Monster{

boolean isDaemon = false;
java.util.Timer melee = new java.util.Timer(isDaemon);

public Monster(){}//Monster

public void setScheduleMelee(long period){
System.out.println("..setScheduleMelee");
period = 4000;
java.util.TimerTask dummyTask = new MonsterTask();
java.util.Date nowDate = new java.util.Date();
melee.scheduleAtFixedRate(dummyTask,period,period);
}//setScheduleMelee

public static void main (String args[]) {
System.out.println("main..");
}//main
}//Monster

////////////////////////////code///////////////////
package atreides.monsters;

import java.util.*;

public class TestDriveMonsters{

private static List<Monster> list = new ArrayList<Monster>();

public TestDriveMonsters(){
System.out.println("TestDriveMonsters..");
}//TestDriveMonsters

public static void makeMonsters(){
System.out.println("..makeMonsters");
for (int i=0; i<5; i++){
list.add(new Monster());
}//for
}//makeMonsters

public static void setTimer(){
System.out.println("..addTimer");
for (Monster monster:list){
}//for
}//setTimer

public static void main (String args[]) {
System.out.println("..main");
makeMonsters();
setTimer();
}//main
}//TestDriveMonsters

//////////////////////code///////////////

package atreides.monsters;

public class MonsterTask
extends java.util.TimerTask{

public MonsterTask(){}//MonsterTask

public boolean cancel(){
return false;
}//cancel

public void run(){
System.out.println("..run");
}//run

public long scheduledExecutionTime(){
return 0;
}//scheduledExecutionTime

public static void main (String args[]) {
System.out.println("main..");
}//main
}//MonsterTask

thanks,

Thufir Hawat



Relevant Pages