I´ve added the other getters and setters. (You use getters and setters to control the values that may be placed into that variable. For example, you couldn't have a negative cost of a product. So you could create some code that checks to see if the value passed to the set method it negative. If it was negative, don't assign the number. You could also check for not number values being passed.
class com.ahfx.Product {
//Instance Variables
private var price:Number;
private var desc:String;
private var quantity:Number;
function Product() {
}
function getPrice():Number {
return price;
}
function setPrice(myVar:Number):Void {
price = myVar;
}
function getQuantity():Number {
return quantity;
}
function setQuantity(quantity:Number):Void {
this.quantity = quantity;
}
function getDesc():String {
return desc;
}
function setDesc(myVar:String):Void {
desc = myVar;
}
}
Now we will add one other method, getTotal. This method will return a number that will tell us the total cost of this product (price times quantity)
class com.ahfx.Product {
//Instance Variables
private var price:Number;
private var desc:String;
private var quantity:Number;
function Product() {
}
function getPrice():Number {
return price;
}
function setPrice(myVar:Number):Void {
price = myVar;
}
function getQuantity():Number {
return quantity;
}
function setQuantity(quantity:Number):Void {
this.quantity = quantity;
}
function getDesc():String {
return desc;
}
function setDesc(myVar:String):Void {
desc = myVar;
}
function getTotal():Number {
return price*quantity;
}
}
This method takes no parameters and returns a Number. You see that it will return price*quantity. Next we will examine the Flash to use this class.
SUPPORT THIS SITE If this tutorial helped you, show your support and send me something from my wish list. Click on an item below and then choose Adam Hayes (Gift Registry Address) for the Ship to Address. It is that easy!