Tuesday, August 18, 2009

JavaFX Script Classes and Objects

JavaFX supports the Class and object notion as in Java. A class is written almost in a similar manner as in Java. It also has the concept of Access modifiers. To write a class, open a new script file and name it User.fx

User.fx

//public access, so it is visible to everyone
public  class User {
     var name: String;
     var age: Integer;

    //Getters and setters function
    public function setName(n:String) {
        name = n;
    }

    public function getName() : String{
        return name;
    }

    public function setAge(a:Integer){
        age = a;
    }

    public function getAge() : Integer{
        return age;
    }
}

To use this class, we can declare an instance of this class. The instance is known as object similar to Java. Write a Main.fx, where we will use this class

Main.fx

//Define the object for User class
var user  = User{}

//Set the values
user.setName("SuperMan");
user.setAge(20);

//Print the values
println("The name is : {user.getName()}");
println("The age is : {user.getAge()}");

The User properties can be accessed directly also. For that we will have to change the access modifiers. In that case, User.fx would look like

User.fx

public  class User {
     public var name: String;
     public var age: Integer;
}

Main.fx

var user = User{
    name : "SuperMan";
    age : 20;
}

println("The name is : {user.name}");
println("The age is : {user.age}");

Inheritance

The concept of inheritance also exists in JavaFX, which is similar to Java. The extended class inherits the properties and behavior of base class. If the creation of instance of base class needs to be prevented, the base class can be defined as abstract.

Student.fx

public class Student extends User{
    public var marks : Double;
}

Main.fx

var student = Student{
    name : "SpiderMan";
    age :  25;
    marks : 99.9;
}

println("The name is : {student.name}");
println("The age is : {student.age}");
println("The marks are : {student.marks}");

Mixin Inheritance

JavaFX does not support multiple inheritance. However JavaFX supports mixin classes which can be used to support multiple inheritance. Mixin classes are liker interfaces in Java, which cannot be instantiated. However a class can extend from multiple mixin classes. The difference with mixin and interface is that a mixin can have variables and implemented functions, however the interface can only have static final variables and function signatures.
Functions
Defining a mixin class

MixinOne.fx

public mixin class MixinOne {
   //The function is of abstract type and return type is void
   public abstract function foo() : Void;
}

The extended class

ExtendedOne.fx

public class ExtendedOne extends MixinOne{

   //The override is used to tell that this function is getting overridden 
   override public function foo() : Void{
        println("From ExtendedOne");
    }
}

In Main.fx

var extended = ExtendedOne {}

//calls the extended function
extended.foo();

We can provide a default body also to the mixin. This is different from java interfaces, where interfaces function are always abstract.

Modify MixinOne.fx

public mixin class MixinOne {
       public function foo() : Void{
         //function body
         println("From mixin One");
       }
}

Define another extended class

ExtendedTwo.fx

public class ExtendedTwo extends MixinOne{
  //We are not defining any function. It will inherit from MixinOne
}

In the main class

var extended = ExtendedOne {}
extended.foo(); //Will print "From ExtendedOne"

var extendedTwo = ExtendedTwo {}
extendedTwo.foo(); //Will print "From mixin One"

Variables

Contrary to Java Interfaces, JavaFX mixin can have variables also, which behave similar to function as outlined above. They can be given default values which can be overridden.

MixinOne.fx

public mixin class MixinOne {

  //Variable with default value
  public var name : String = "MixinOne" ;
}

ExtendedOne.fx

public class ExtendedOne extends MixinOne{

  //Variable overridden. No need to mention type as it is inherited    
  public override var name = "ExtendedOne" ;
}

ExtendedTwo.fx

public class ExtendedTwo extends MixinOne{
  //Inherits the variable from MixinOne
}
In the main class

var extendedOne = ExtendedOne {}
println("{extendedOne.name}"); //Will print "ExtendedOne"

var extendedTwo = ExtendedTwo {}
println("{extendedTwo.name}"); //Will print "MixinOne"

Extending From multiple mixin
A class can be extended from multiple mixin also. Define a new mixin class

MixinTwo.fx

public mixin class MixinTwo {

   //Note that the function is same as MixinOne
   public function foo() : Void{
        println("From MixinTwo");
   }
}

In the main class

var extendedTwo = ExtendedTwo {}

//To resolve ambiguity, we specify which foo() we want to call
(extendedTwo as MixinOne).foo();
(extendedTwo as MixinTwo).foo();

More on JavaFX

No comments:

Post a Comment