标签: method-invocation

我可以检查是否返回了void方法?

我只是想问一下,如果有可能通过调用来检查void方法是否"取消"了自己return;

例如在我的主要调用中calculate(myArray);,其定义如下:

public static void calculate(Object[] array) {
    if (array == null)
        return;
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)

他们是否有办法知道,如果它回来了?我的想法是制作一个"全局"布尔值,在我们返回之前将其更改为true,然后在main中检查它的值,或者只是将返回类型更改为类似于int的类型以及它在开始时返回时我们使用return -1;和在结束时方法return 0;

两者都有可能,但我认为它们都不是很好的风格.还有其他选择吗?

java arrays return method-invocation

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

为什么递归调用此函数不会抛出NullPointerException

我的问题来自这个帖子.

考虑以下代码:

public class Test {    
    static Function<Integer, Integer> fibLambda = null;
    public static void main (String[] args) { 
        fibLambda = n -> n <= 2 ? 1 : fibLambda.apply(n - 1) + fibLambda.apply(n - 2); 
        System.out.println(fibLambda.apply(6));
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的输出是8.

我没有得到的是如何fibLamdba初始化?似乎我完全错过了方法调用是如何完成的,因为我虽然这个代码会产生一个NPE.

希望我的问题很明确

java method-invocation java-8

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

Groovy运行时方法拦截

我正在玩Groovy,我想知道,为什么这段代码不起作用?

package test

interface A {
    void myMethod()
}

class B implements A {
    void myMethod() {
        println "No catch"
    }
}

B.metaClass.myMethod = {
    println "Catch!"
}

(new B()).myMethod()
Run Code Online (Sandbox Code Playgroud)

它打印出来No catch,而我希望它打印出来Catch!.

groovy metaclass method-invocation interception

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

在if语句中使用方法调用程序

我要做的是检查是否选中了列表框中的项目.

该方法正在一个单独的线程上运行,所以我需要使用我相信的方法调用程序.

string list = "";
lbxList.Invoke(new MethodInvoker(delegate { list = lbxList.SelectedItem.ToString(); }));

if (list != null)
{
 //do something
}
Run Code Online (Sandbox Code Playgroud)

如果所选项目为null,则此代码将会爆炸,因为字符串列表不会保留它,因此我需要一种方法将前2行组合成if语句检查null.

谢谢

c# multithreading listbox method-invocation

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

我可以创建一个在python中接收任意方法调用的对象吗?

在python中,我可以创建一个Class,在实例化时,可以接收任意方法调用吗?我已经读过这篇文章,但无法将这些文章放在一起

我想这与它有关attribute lookup.对于一个类Foo:

class Foo(object):
  def bar(self, a):
    print a
Run Code Online (Sandbox Code Playgroud)

class属性可以通过print Foo.__dict__给出来获得

{'__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__module__': '__main__', 'bar': <function bar at 0x7facd91dac80>, '__doc__': None}
Run Code Online (Sandbox Code Playgroud)

所以这段代码是有效的

foo = Foo()
foo.bar("xxx")
Run Code Online (Sandbox Code Playgroud)

如果我打电话foo.someRandomMethod(),AttributeError: 'Foo' object has no attribute 'someRandomMethod'就会结果.

我希望foo对象接收任何随机调用,默认为no-op,即.

def func():
    pass
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?我希望这种行为模拟一个对象进行测试.

python method-invocation

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

Java中的动态方法查找

我试图在Java中调用Dynamic方法调用,而不能理解为什么java不允许我从子类调用方法而不是超类方法.

例如:如果我有2个班级TestTest2.Test2 继承自Test类

该方法someFunction()在子类中被重写:

考试班

public class Test {

    public Test(){
        System.out.println("I am Test class constructor called with no values");
    }

    public void someFunction(){
        System.out.println("I am some function belonging to Test Class");
     }
  }
Run Code Online (Sandbox Code Playgroud)

和Test2类:

public class Test2 extends Test{

    public Test2(){
       System.out.println("Constructor of Test2 with no values");
    }

    public void somFunction(){
        System.out.println("I am someFunction overridden in Test2");
    }
}
Run Code Online (Sandbox Code Playgroud)

所以当我尝试以这种方式实例化Test类时:

    Test t1 = new Test2();
    t1.someFunction(); // this should call Test2.someFunction()
Run Code Online (Sandbox Code Playgroud)

我得到的输出是: …

java inheritance overriding method-invocation

0
推荐指数
1
解决办法
5163
查看次数