Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从参数调用方法_Java - Fatal编程技术网

Java 从参数调用方法

Java 从参数调用方法,java,Java,很抱歉,如果这是一个重复的问题,我希望能够从其他方法调用构造函数参数列表中定义的方法 下面的代码不会编译,但这确实是我唯一能想到的描述问题的方法。我也希望我的解释有意义 Main.java .... Class0 instance = new Class0(arg0, arg1, arg2, new Class1(){ //do something //do something else //do more stuff } ) librar

很抱歉,如果这是一个重复的问题,我希望能够从其他方法调用构造函数参数列表中定义的方法

下面的代码不会编译,但这确实是我唯一能想到的描述问题的方法。我也希望我的解释有意义

Main.java

....
Class0 instance = new Class0(arg0, arg1, arg2, new Class1(){
        //do something
        //do something else
        //do more stuff
    }
)
library.go(instance);
....
public final class Main
{
    private static void callIt(final Kapow theCallback)
    {
        theCallback.callbackMethod();
    }

    public static void main(String[] args)
    {
        Kapow kapowObject = new KapowImpl();

        callIt(kapowObject);
    }
}
这里我想说明的一点是,Class0的一个新实例是用一个匿名函数初始化的。 然后将实例传递给库的实例

Class0.java

....
public Class1 action = null;
public Class0(int arg0, int arg1, int arg2, Class1 class) {
     setArg0(arg0);
     setArg1(arg1);
     setArg2(arg2);
     setAction(class);
}
public setAction(Class1 class) {
     action = class;
}
public action() {
     class;
}
....
Class0是从构造函数方法构造的,并将函数设置为action字段,它保持未调用状态,但会存储以备将来使用。 action()调用传递给构造函数的函数

Library.java

....
public void go(Class0 class0) {
    class0.action();
}
....
对于大多数情况,Library.java是我无法控制的,它是第三方库的管道。 go通过其action方法调用实例对象的存储函数(在main中声明)

java中是否远程存在类似的东西?有没有其他方法可以达到同样的效果?

一个“方法类型声明”的好例子是
java.awt.event.ActionListener
(见下文)。在Java8或更高版本中,您可以使用use来简化使用,但原理仍然是一样的——只有一个方法声明的接口代表逻辑方法

/*
 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.awt.event;

import java.util.EventListener;

/**
 * The listener interface for receiving action events.
 * The class that is interested in processing an action event
 * implements this interface, and the object created with that
 * class is registered with a component, using the component's
 * <code>addActionListener</code> method. When the action event
 * occurs, that object's <code>actionPerformed</code> method is
 * invoked.
 *
 * @see ActionEvent
 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/eventmodel.html">Tutorial: Java 1.1 Event Model</a>
 *
 * @author Carl Quinn
 * @since 1.1
 */
public interface ActionListener extends EventListener {

    /**
     * Invoked when an action occurs.
     */
    public void actionPerformed(ActionEvent e);

}
下面是一个关于如何使用该模式的快速示例:

public static void main(String[] args) {
    ActionListener squeezeAction = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Ouch!");
        }
    };
    performAction(squeezeAction);
}

public static void performAction(ActionListener method) {
    method.actionPerformed(null); //invoke method
}
对于lambda表达式(需要JRE 1.8或更高版本),这可以简化为:

public static void main(String[] args) {
    ActionListener squeezeAction = e -> System.out.println("Ouch!");
    performAction(squeezeAction);
}

public static void performAction(ActionListener method) {
    method.actionPerformed(null); //invoke method
}
或作为对现有方法的参考:

public class Test {
    public static void main(String[] args) {
        ActionListener squeezeAction = Test::squeeze;
        performAction(squeezeAction);
    }

    public static void sqeeze(ActionEvent e) {
        System.out.println("Ouch!");
    }

    public static void performAction(ActionListener method) {
        method.actionPerformed(null); //invoke method
    }
}

编辑:假设为java 7.0或更早版本。它在Java8中工作,但lambda表达式很可能是首选

看起来您想要实现一个回调接口

  • 使用方法创建接口
  • 将接口用作方法的参数(在本例中为构造函数)
  • 接口只是对某个对象的引用,因此调用该方法
  • 下面是一些代码:

    java

    public interface Kapow
    {
        void callbackMethod();
    }
    
    kapoimpl.java

    public class KapowImpl implements Kapow
    {
        @Override
        public void callbackMethod()
        {
            System.out.println("Kapow!");
        }
    }
    
    Main.java

    ....
    Class0 instance = new Class0(arg0, arg1, arg2, new Class1(){
            //do something
            //do something else
            //do more stuff
        }
    )
    library.go(instance);
    ....
    
    public final class Main
    {
        private static void callIt(final Kapow theCallback)
        {
            theCallback.callbackMethod();
        }
    
        public static void main(String[] args)
        {
            Kapow kapowObject = new KapowImpl();
    
            callIt(kapowObject);
        }
    }
    

    它无法编译的第一个原因是您不应该使用像
    class
    这样的保留关键字作为var名称。然后,
    action
    应该有返回值。使用lambda或接口声明所需的方法。阅读Java 8中新的lambda功能。我试图将兼容性降低到Java 6,我现在将研究接口,“如果这是一个重复的问题,我很抱歉”-向下投票,因为您没有显示任何研究成果。为清楚起见,这意味着对于我希望使用的每个不同回调,我需要为实现创建单独的类?我不能匿名做这件事?没错。但是如果可能的话,您应该重用
    @functioninterface
    s(在引入lambda表达式的Java8中称为)。