标签: method-invocation

动态调用类方法Args

我正在研究一些我需要能够将args的索引数组传递给方法的东西,就像call_user_func_array工作方式一样.我会使用,call_user_func_array但它不是一个OOP方法,这是不受欢迎的,它需要该方法是静态的,这打破了目标类的OO.

我试过用ReflectionClass但无济于事.您不能调用类的方法的参数,只能调用构造函数.遗憾的是,这不是理想的.

所以我进入了手册页并查看了ReflectionFunction但是没有办法实例化类,将它指向一个方法,然后invokeArgs用它.

使用示例ReflectionFunction(记住,这个问题标记为PHP 5.4,因此语法):

$call = new \ReflectionFunction( "(ExampleClass())->exampleMethod" );
$call->invokeArgs( ["argument1", "argument2"] );
Run Code Online (Sandbox Code Playgroud)

这失败了:

Function (Index())->Index() does not exist
Run Code Online (Sandbox Code Playgroud)

使用示例 ReflectionMethod

$call = new \ReflectionMethod( "ExampleClass", "exampleMethod" );
$call->invokeArgs( new ExampleClass(), ["argument1", "argument2"] );
print_r( $call );
Run Code Online (Sandbox Code Playgroud)

这失败了:

ReflectionMethod Object
(
    [name] => Index
    [class] => Index
)
Run Code Online (Sandbox Code Playgroud)

参数永远不会传递给方法.

期望的结果是:

class ExampleClass() {
    public function exampleMethod( $exampleArg1, $exampleArg2 ){
        // do something here
        echo "Argument 1: …
Run Code Online (Sandbox Code Playgroud)

php oop dynamic method-invocation

6
推荐指数
2
解决办法
5001
查看次数

如何通过Java中的反射调用代理方法(Spring AOP)?

界面:

public interface Manager {
  Object read(Long id);
}
Run Code Online (Sandbox Code Playgroud)

实现此接口的类:

@Transactional
Public class ManagerImpl implements Manager {
  @Override  
  public Object read(Long id) {
    //  Implementation here  
  }
}
Run Code Online (Sandbox Code Playgroud)

ManagerImpl的一个方面:

@Aspect
public class Interceptor {
  @Pointcut("execution(public * manager.impl.*.*(..))")
  public void executionAsManager() {
  }

  @Around("executionAsManager()")
  public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
    //  Do some actions
    return joinPoint.proceed();
  }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

@RestController()
public class Controller {

  @Autowired
  private Manager manager;

  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public Object read(@PathVariable Long id) { …
Run Code Online (Sandbox Code Playgroud)

java reflection proxy spring method-invocation

6
推荐指数
2
解决办法
4572
查看次数

如何检查是否使用对象调用方法调用子例程

您可以使用以下示例中的两种语法调用子例程作为方法.

但您也可以不将其作为对象调用.

#====================================================
package Opa;
sub opa{
    $first= shift;
    $second= shift;
    print "Opa $first -- $second\n";
}

package main;
# as object:
Opa->opa("uno");
opa Opa ("uno");
# not as object
Opa::opa("uno","segundo");
Opa::opa("Opa","uno");
#====================================================
Run Code Online (Sandbox Code Playgroud)

有一种方法,从子程序内部,知道"一般",子接收了什么样的调用?

perl object subroutine method-invocation

6
推荐指数
1
解决办法
135
查看次数

Raku 类中的 $.attribute 和 self.attribute 有什么区别?

例如,在此类中,Foo两个方法bc返回相同的值:

class Foo {
    method a { 42     }
    method b { $.a    }
    method c { self.a }
}

my $foo = Foo.new;
say $foo.a; #=> 42
say $foo.b; #=> 42
say $foo.c; #=> 42
Run Code Online (Sandbox Code Playgroud)

我注意到我可以对其他 sigil 和 twigil 组合做同样的事情,例如@.scoresvs self.score%.matchesvsself.matches等。

编辑:我将这个问题标记为重复,但是正如 librasteve 指出的那样,我没有意识到另一个问题的答案中提到的微妙区别不是焦点,并且可能很容易被错过,特别是对于 Raku 的新手来说。

methods method-invocation raku

5
推荐指数
1
解决办法
61
查看次数

如何将哈希项映射到方法参数?

我有一个带有冗长可选参数列表的方法,例如:

def foo(foo = nil, bar = nil, baz = nil, qux = nil)
    # no-op
end
Run Code Online (Sandbox Code Playgroud)

我认为调用方法并将分割散列作为参数传递会通过将键与方法参数匹配来将散列项映射到参数:

params = { bar: 'bar', foo: 'foo' }
foo(*params)
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我在使用拆分哈希调用方法后检查局部变量时,如果我传入一个split数组,我会得到我所期望的,但这不是我希望的:

foo == [:bar, 'bar'] # hoped: foo == 'foo'
bar == [:foo, 'foo'] # hoped: bar == 'bar'
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

ruby method-invocation

4
推荐指数
1
解决办法
1754
查看次数

Java:如何调用名称存储在字符串变量中的函数

我有一个存储在变量中的泛型字符串String func = "validate";,我想调用该名称的函数,即我需要调用validate()函数.
我的意思是说我会将变量传递给某个函数,
public void callFunc(String func){
....
}
所以上面的函数应该调用相应的函数,函数名作为参数传递给callfunc().

java reflection method-invocation

3
推荐指数
1
解决办法
7167
查看次数

Scala:如何创建一个允许我在调用时使用点表示法的函数?

尽管多次阅读Scala样式指南 - 方法调用,但我已经对此感到困惑了一段时间.

我希望能够调用此方法

def foldRVL[A,B](l: List[A], z: B)(f: (A, B) => B) = //"Right-Via-Left"
  l.reverse.foldLeft(z)((a, b) => f(b, a))
Run Code Online (Sandbox Code Playgroud)

使用这样的点符号List(1,2,3).foldRVL(0)(_ + _).

而不是这样的:foldRVL(List(1,2,3), 0)(_ + _).

此外,有时我看到的代码显示的方法实际上是在签名中采用零参数,或者比我预期的要少一个参数,并且仍然使用点符号正确地获取参数.这是如何运作的?我问这个,因为这些方法使用点符号,所以也许如果我写了类似的东西,我可以解决我的问题.

scala this infix-notation method-invocation

3
推荐指数
1
解决办法
642
查看次数

方法局部内部类

public class Test {
    public static void main(String[] args) {

    }
}

class Outer {
    void aMethod() {
        class MethodLocalInner {
            void bMethod() {
                System.out.println("Inside method-local bMethod");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人能告诉我如何打印消息bMethod吗?

java inner-classes method-invocation

2
推荐指数
1
解决办法
4648
查看次数

为什么我不能通过字符串调用此方法?

反思新手的问题.我在Windows窗体中有一个方法:

private void handleOrderCode()
{
  //...do stuff
}
Run Code Online (Sandbox Code Playgroud)

我试图以下列方式打电话:

Type t = this.GetType();
MethodInfo mi = t.GetMethod("handleOrderCode");
if (mi != null) mi.Invoke(this, null);
Run Code Online (Sandbox Code Playgroud)

我已经确认"这个"不是空的.字符串"handleOrderCode"已被硬编码的空间将在此工作时替换为字符串变量.但是,目前"mi"在最后一行的if语句中求值时始终为null.

那么我做错了什么?

.net c# reflection method-invocation

2
推荐指数
2
解决办法
173
查看次数

通用方法调用

如果我们有通用方法

class SClass{
    public static <T> ArrayList<T> listFactory(){ return new ArrayList<T>(); }
}
Run Code Online (Sandbox Code Playgroud)

我们可以T在调用此方法时定义type-parameter explicit.

SClass.<?>listFactory();//compile error
SClass.<List<?>>listFactory();//ok
Run Code Online (Sandbox Code Playgroud)

为什么我们不能listFactory用类型参数调用?,但可以用List<?>

java generics method-invocation

2
推荐指数
1
解决办法
125
查看次数