Introduction | Your first interface | Building a model | Advanced features | Resources

4.11 Using simClass

This section shows you how to extend simClass to easily reuse code for more complex simulations. The model and interface used in this section can be downloaded here: trainingSimClass.zip.

The main class in your simulation will always extend mainSimClass (which is itself a subclass of simClass) but other classes can also be incorporated into your code without inheriting from simClass. The advantage of building on simClass is that you can register properties directly and force explicit property updates within any class that inherits from simClass (however, action and file methods cannot be registered).

In the example below, a Die class has been created that extends simClass to make the properties to each die accessible at the user interface. Click on the hand to roll the dice and on the blackboard rubber to clear the score and start again.

When a class is extended directly from simClass it must provide a constructor that takes a single parameter of a mainSimClass (which must in turn be passed to the superclass constructor). Properties can then be registered within the constructor in the normal way.

In the example, the constructor of the Die class looks like this:

	//constructor must take a mainSimClass
	public Die(mainSimClass main) {
		//must pass a mainSimClass to the superclass constructor
		super(main);
		//register properties
		registerProperty("Number","numberThrown",OUTPROP);
	}
	

When the mainSimClass creates an instance of simClass it must pass itself as a parameter to the simClass constructor.

d1 = new Die(this);

When you import a class into the Interface Builder that uses classes that extend simClass you will be able to view and manipulate registered variables in each instance of the simClass individually.