Java Reference
In-Depth Information
Creating and using JavaFX classes
JavaFX is an object-oriented scripting language. As such, object types, represented as classes,
are part of the basic constructs of the language. This section shows how to declare, initialize,
and use JavaFX classes.
Getting ready
If you have used other scripting languages such as ActionScript, JavaScript, Python, or PHP,
the concepts presented in this section should be familiar. If you have no idea what a class is
or what it should be, just remember this: a class is code that represents a logical entity (tree,
person, organization, and so on) that you can manipulate programmatically or while using your
application. A class usually exposes
properties
and
operations
to access the state or behavior
of the class.
How to do it...
Let's assume we are building an application for a dealership. You may have a class called
Vehicle
to represent cars and other type of vehicles processed in the application. The next
code example creates the
Vehicle
class. Refer to
ch01/source-code/src/javafx/
Vehicle.fx
for full listing of the code presented here.
1. Open your favorite text editor (or fire up your favorite IDE).
2. Type the following class declaration.
class Vehicle {
var
make;
var
model;
var
color;
var
year;
function
drive () : Void {
println("You are driving a "
"{year} {color} {make} {model}!")
}
}
3. Once your class is properly declared, it is now ready to be used. To use the class,
add the following (highlighted code) to the file:
class Vehicle {
...
}