Reputation: 242
I have 2 classes, named Class1
and Class2
.
How would I use a function from Class1
, in Class2
?
Upvotes: 0
Views: 1705
Reputation: 48793
You can call the function/property
of Class2
from Class1
, if that function/property
is 'visible' for Class1
. Access to properties/methods of class is controlled by specific attribute,which can have these values: internal (default),private,public,protected,...
Check this.
Then if it is 'visible', all you need,is to have reference to class(to access static
properties/methods) or to instance of class(to access non-static
properties/methods).
Upvotes: 0
Reputation: 6349
there are lots of different ways to do this.
Call a method directly on a class
Class1
class Class1{
public function Class1(){
var class2:Class2 = new Class2(); //create a new Class2 object
class2.CallMe(); //call the CallMe function on Class2, note the . to indicate CallMe is a property of Class2
}
}
Class2
class Class2{
public function Class2(){ //class 2 constructor. This is always called when you create a new Object()
}
public function CallMe():void{ //note this function is public
trace("I'm class 2");
}
}
Pass a ref to a function from another class
Class1
class Class1{
public function Class1(){
var class2:Class2 = new Class2(this.CallBack); //create a new Class2 object
//note I didn't add () to the end of CallBack, I just used the function name
}
private function CallBack(aString:String):void{ //note we can use private here
trace(aString) //I'm from Class2
}
}
Class2
class Class2{
public function Class2(aFunction:Function){ //class 2 constructor. Here we an accept the function
aFunction("I'm from Class2"); //note I can pass a string back to class1
}
}
You can also extend classes and call methods on the super class
Class1
class Class1 extends Class2{
public function Class1(){
super.CallFromClass(); //I'm super
}
}
Class2
class Class2{
public function Class1(){
}
public function CallFromClass():void{
trace("I'm super");
}
}
Take a look at this link for further reading: http://www.adobe.com/devnet/actionscript/articles/oop_as3.html
Upvotes: 0
Reputation: 39456
To access a function from a class you need to create an instance of it via the new
keyword (unless the function is static
). This instance could be created within Class2:
public class Class2
{
// The property holding your instance is defined here so you can access
// it within any method of this class, as this is defined at class-level
// rather than within a function (like the constructor).
private var class1:Class1;
/**
* Class2's constructor.
* (Called automatically when an instance of Class2 is created).
*/
public function Class2()
{
// Create an instance of Class1 and assign it to the class-level
// property "class1" (defined above).
class1 = new Class1();
// You can call methods of Class1 now, via class1.
class1.someMethod();
}
}
Upvotes: 2
Reputation:
// Main.as
package {
public class Main() {
public function Main() {
}
public function hello():void {
trace("Hello from class Main"):
}
}
}
//Hi.as
package {
public class Hi() {
private var _main:Main;
public function Hi() {
}
public function fromMain():void {
_main = new Main(); //initialize an object of class Main()
_main.hello(); //call methods
}
}
}
Upvotes: 0