Interface is a class used as a pattern, or template for classes with similar functions, that must respect a certain basic structure.
Synthesized, the Interface is a class with a list of required methods that must be created in the classes where it is implemented.
All the methods specified in the "Interface" must be defined in the classes where it's applied, having the same name, data type and parameters as indicated in the "Interface".
The interface class is created in the same way as other types of classes, in a "package", saved in a separate AS document. The difference is that, instead of the keyword "class" you use the keyword "interface"; also, in its body you only write a list of methods, without code instructions.
The general syntax is:
package { public interface InterfaceName { function methodName(prop1:DataType, prop2:DataType, ...):DataType; function otherMethod(prop1:DataType, prop2:DataType, ...):DataType; ........... } }- When declaring the methods in an interface, their code or curly brackets are not added, neither an attribute, because in an interface class are added only the methods that will have the "public" attribute.
package { // Create interface public interface ITest { // A list with required methods function Links():void; function Tutorials(gen:String, nota:int):String } }- This code must be added in an "ActionScript 3.0 Class" document (or "ActionScript 3.0 Interface"), write "ITest" at the class name and the file with this code must be saved with the name "ITest.as", in the same folder as the classes which will implement this interface.
Once the "interface" was saved, you can create classes that implement the methods specified in the interface.
To implement an interface, use the keyword implements and the interface name after the class name:
public class ClassName implements InterfaceName { // Instructions }- "ClassName" must contain all the methods defined in "InterfaceName", with the "public" attribute, the number of parameters and their DataType established in the interface. The class can also contain other methods.
package { // Create class that implements the ITest interface public class webDevelopment implements ITest { // Define a protected property protected var site:String = 'coursesweb.net'; /* Define the required methods (Links and Tutorials), specified in interface */ // Returns in Output the value of the 'site' property public function Links():void { trace(this.site); } // Return the value of a variable that takes the transmitted arguments public function Tutorials(gen:String, note:int):String { var re:String = gen+'-'+note; return re; } // creates an additional method // Displays in Output the argument received public function Diverse(val:*):void { trace(val); } } }- The required methods ("Links()" and "Tutorials()") respect the exact parameters (number, order, data type) set in the "ITest" interface. Other methods (in this case "Diverse") and properties are optional, depending on each class role.
// Create an object instance of webDevelopment class var obj:webDevelopment = new webDevelopment(); // Call the methods obj.Links(); // coursesweb.net trace(obj.Tutorials('Flash', 8)); // Flash-8 obj.Diverse(2010); // 2010- Because the "webDevelopment" class meets the conditions specified in the implemented interface (ITest), the script is functional.
The interface can also be applied as a data type to variables (or function parameters).
Then, the variable (or parameter) can represent an instance of the class which implements that "interface".
You can understand it better from the following example, where we create another class (called WebProgramming) that implements the interface "ITest".
package { // Create class that applies ITest interface public class WebProgramming implements ITest { // Defining a protected property protected var adr:String = 'coursesweb.net/'; /* Defining the required methods (Links and Tutorials), specified in interface */ // Returns a string in Output public function Links():void { trace('Have a good life'); } // Returns the value of a variable "re" public function Tutorials(gen:String, nr:int):String { var re:String = nr+'-'+ this.adr+gen; return re; } } }In the FLA document, write the following code in the "Actions panel".
// Create object instaces of the classes which implements 'ITest' var web_development:webDevelopment = new webDevelopment(); var web_programming:WebProgramming = new WebProgramming(); // Create a function with a parameter that has "ITest" interface as data type function courses(cls:ITest):void { // Call the methods set in ITest, through the "cls" parameter cls.Links(); trace(cls.Tutorials('flash', 4)); } // Calls the courses() function, passing for parameter the instances created above courses(web_development); // In Output displays: "coursesweb.net" and "flash-4" courses(web_programming); // In Output displays: "Have a good life" and "4-coursesweb.net/flash"- Notice that calling the function with different arguments, reprezenting the class instances, the function uses the parameter "cls" as instance of the class (because it has "ITest" as data type), and can call the same methods ("Links" and "Tutorials") for each argument, because these methods are specified in "interface", so they must exist in each class that implements that interface, with the same type of parameters.
public interface ChildInterface extends ParentInterface { // List of additional methods }- The "ChildInterface" will contain the list of methods inherited from the "ParentInterface", and whatever else is added in it. It will not affect the ParentInterface.
public ChildClass extends ParentClass implements Interface- "ChildClass" can be an extension of any class, doesn't matter if the "ParentClass" implements or not an Interface.
public class ClassName implements Interface1, Interface2
If you modify an Interface which is already applied to a class, that class will not work.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net