简体   繁体   English

从某些refreence变量的方法(不是该类的子级)中了解类的方式是什么

[英]What's the way to know the class from the method of some refrence variable ( not a child of that class )

What can be the way to know the class name "Abc" from where the function of a reference variable "def" has been called ? 从调用变量“ def”的位置知道类名“ Abc”的方法是什么? Except these two ways : 除了以下两种方式:

1) Adding the def as a child of Abc 1)将def添加为Abc的子代

2) Injecting reference of Abc into Def prior or during the function call. 2)在函数调用之前或期间将Abc的引用注入Def。

here is the class Abc : 这是Abc类:

public class Abc
{
   var def:Def ; 
   public function Abc()
   {

         def = new Def();
        def.myfun() ;
   }



}


public class Def 
{

  public function Def()
  {
  }
  public function myfun():void 
  {

     // Code to know, this function has been called from class named Abc ; 
  }

}

No, that is not possible. 不,那是不可能的。 It is also useless. 这也没用。 The closest thing you can do to what you want is to get a reference to the function calling Abc.myfun() via arguments.callee : 您可以根据需要执行的最接近的操作是通过arguments.callee获得对调用Abc.myfun()的函数的引用:

public function myfun():void
{
    var callee:Function = arguments.callee;
}

Though this won't have any useful information attached like the function or class name. 尽管它不会附带任何有用的信息,例如函数或类名。

Maybe like this: 可能是这样的:

public class Abc
{
   var def:Def;

   public function Abc()
   {
        def = new Def();
        def.myfun( true ) ;
   }
}


public class Def 
{
  public function Def()
  {
  }

  public function myfun(fromAbc:Boolean=false):void 
  {
    if( fromAbc == true )
    {
        // Code to know, this function has been called from class named Abc ;
    }
  }
}

You could get the information from the stacktrace, but that would only work in debug mode. 您可以从堆栈跟踪中获取信息,但这仅适用于调试模式。

There isn't really a clean way to do it, because you shouldn't need to do it. 确实并没有一种干净的方法,因为您不需要这样做。 If you explained a bit more about your situation / goal there is probably a simple answer (requiring a small redesign) that will help you. 如果您对自己的情况/目标有更多的解释,那么可能会有一个简单的答案(需要重新设计)对您有所帮助。 (cant post comments) (无法发表评论)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM