-
for discussion: OOP OR Function
Hello all,
Why should one use OOP structure when writing a programme? A "class" is just about series of actions that will result in performing something, the "function" is the same.
Can someone write small piece of code just to illustrate the difference.
Your inputs are highly appreciated!
-
Personally I will always use functions over classes in AS2.
If I remember correctly, classes in AS2 can only be declared in AS files, and the last time I looked at using AS files (correct me if I am wrong) but you have to compile every FLA that includes the AS file. So If i had a common classes AS file used in a dozen FLA's they would all need to be compiled every time I changed a class. Whereas a common functions FLA... need I go on.
Below is a class example from Adobe's online documentation.
//************************************************** ************************************
// Filename Plant.as
class Plant {
// Define property names and types
var leafType:String;
var bloomSeason:String;
// Following line is constructor
// because it has the same name as the class
function Plant(param_leafType:String, param_bloomSeason:String) {
// Assign passed values to properties when new Plant object is created
this.leafType = param_leafType;
this.bloomSeason = param_bloomSeason;
}
// Create methods to return property values, because best practice
// recommends against directly referencing a property of a class
function getLeafType():String {
return leafType;
}
function getBloomSeason():String {
return bloomSeason;
}
}
var pineTree:Plant = new Plant("Evergreen", "N/A");
// Confirm parameters were passed correctly
trace(pineTree.getLeafType());
trace(pineTree.getBloomSeason());
//************************************************** ************************************
Below is a quick test to see if I could replicate this class using a function
//************************************************** ************************************
function Plant(param_leafType, param_bloomSeason) {
var leafType = param_leafType;
var bloomSeason = param_bloomSeason;
this.getLeafType = function() {
return leafType;
};
this.setLeafType = function(aValue) {
leafType = aValue;
};
this.getBloomSeason = function() {
return bloomSeason;
};
this.setBloomSeason = function(aValue) {
bloomSeason = aValue;
};
}
var pineTree = new Plant("Evergreen", "N/A");
var pineTree2 = new Plant("Evergreen2", "N/A2");
trace(pineTree.getLeafType());// returns Evergreen
trace(pineTree.getBloomSeason());//returns N/A
trace(pineTree2.getLeafType());// returns Evergreen2
trace(pineTree2.getBloomSeason());// returns N/A2
pineTree.setLeafType("Evergreen3");
trace(pineTree.getLeafType());// returns Evergreen3
trace(leafType); // returns undefined, this is expected. Just checking leafType is still encapsulated after the set
//************************************************** ************************************
Nice seems to function the same way the class does.
So the answer to your question, Why should one use OOP structure when writing a programme? Well because you really don’t want duplicated functionality and do want reusable easily attached functionality which can be easily achieved using well structured functions and modular FLA / movie clip objects instead of classes. IMO
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|