The JavaFX API and provides us with a large number of classes that we use in our applications, however we can also define your own classes, this section will concentrate on defining a few classes and see new variants provides us the language.
A class is declared using the class keyword and class name class
Address
{var street: String;
var city: String;
var state: String;
var zip: String;}
can see here we are defining the Address class which consists of 4 variables that now
def address = { Street Address: 1 Main Street "; city," Santa Clara " state: "CA";
zip: "95050"}
Above is the creation of an instance of the Address class, which initializes its various properties. Abstract Classes
As in the Java language can define a class as abstract, remember that a class is abstract if it is defined as abstract class because it holds one or more methods (functions if we refer to JavaFX), and if not have them and just mark the class as abstract.
abstract class Account {var
accountNum: Integer;
var balance: Number;
getBalance
function (): Number {
return balance;} function deposit (amount: Number): Void { balance + = Amount; } function
Withdraw (amount: Number): Void { balance -= Amount;
}}
Here we just define our abstract class that provides all necessary functionality but for some reason we decided to mark it as abstract as So we do not care for example, but it is good to clarify that this class could be concrete. To make a class inherit from another we use the word extends
and after that as we add more properties or writing functions or what we need to do.
SimpleAccount
class extends Account {var
hasOverDraftProtection: Boolean;
override function
Withdraw (amount: Number): Void {
if (balance-Amount \u0026lt;0 and hasOverDraftProtection) {
balance = 0;} else {
balance -= Amount; / / may result in negative account balance!
}}}
SimpleAccount In the example we see how to implement inheritance, we can also see the override switch
which is responsible to overwrite a function within the same function we are overwriting see how we use super to access the implementation of parent class.
far we have found many differences with Java but it's time to talk Mixin class. Mixin Classes
mixin classes would be a mix of Java interfaces with abstract classes because they are left to implement and define the implementation of the functions, but we can not instantiate mixin classes.
Example:
package classpkg; mixin class MyNameMixin { var firstName = "John"; var lastName = "Doe"; function printName(){
println("My name is: {firstName} {lastName}");
}
}
mixin class MyAddressMixin { var address = "1 Main Street, Anytown USA";
function printAddress(){
println("My address is: {address}");
}
}
class MyContact extends MyNameMixin, MyAddressMixin { }
def myContact = MyContact{};
myContact.printName();
myContact.printAddress();
In this example we are defining two mixin classes which have properties and functions, and although you can not create new instances of them can use it to achieve multiple inheritance.
I leave a list of rules on classes:
Mixin classes can not be instantiated
Mixin classes can only inherit from classes or interfaces Mixin
Mixin classes can extend any number of interfaces or classes
Mixin
JavaFX classes can only inherit from a class because JavaFX is based on Java
Well with this we end this post, the next topic will be on the visibility modifiers, I hope you have understood everything and have not been questions, discuss anything
Greetings
0 comments:
Post a Comment