2008年12月13日土曜日

JavaFX Classes

The Customer Example

def customer = Customer {
firstName: "John";
lastName: "Doe";
phoneNum: "(408) 555-1212"
address: Address {
street: "1 Main Street";
city: "Santa Clara";
state: "CA";
zip: "95050";
}
}

customer.printName();
customer.printPhoneNum();
customer.printAddress();

class Address {
var street: String;
var city: String;
var state: String;
var zip: String;
}

class Customer {
var firstName: String;
var lastName: String;
var phoneNum: String;
var address: Address;

function printName() {
println("Name: {firstName} {lastName}");
}

function printPhoneNum(){
println("Phone: {phoneNum}");
}

function printAddress(){
println("Street: {address.street}");
println("City: {address.city}");
println("State: {address.state}");
println("Zip: {address.zip}");
}
}



Inheriting from Other Class
他のクラスを継承する

abstract class Account {

var accountNum: Integer;
var balance: Number;

function getBalance(): Number {
return balance;
}

function deposit(amount: Number): Void {
balance += amount;
}

function withdraw(amount: Number): Void {
balance -= amount;
}
}

ここの abstract は、Account objects は直接作成できないことを意味している
Account を継承したクラスは次のようになる

class SavingsAccount extends Account {

var minBalance = 100.00;
var penalty = 5.00;

function checkMinBalance() : Void {
if(balance < minBalance){
balance -= penalty;
}
}
}

この場合、Account の変数と関数はそのまま保持されて、新たに minBalance と penalty 変数と checkMinBalance 関数が追加されている

Account の関数を上書きするには override を使う

class CheckingAccount extends Account {

var hasOverDraftProtection: Boolean;

override function withdraw(amount: Number) : Void {
if(balance-amount<0 and hasOverDraftProtection){

// code to borrow money from an
// overdraft account would go here

} else {
balance -= amount;
// may result in negative account balance!
}
}
}

0 件のコメント:

コメントを投稿