相关疑难解决方法(0)

Java Pass方法作为参数

我正在寻找一种通过引用传递方法的方法.我知道Java不会将方法作为参数传递,但是,我想获得一个替代方案.

我被告知接口是将方法作为参数传递的替代方法,但我不明白接口如何通过引用充当方法.如果我理解正确,接口只是一组未定义的抽象方法.我不希望每次都发送需要定义的接口,因为几种不同的方法可以使用相同的参数调用相同的方法.

我想要完成的是类似的事情:

public void setAllComponents(Component[] myComponentArray, Method myMethod) {
    for (Component leaf : myComponentArray) {
        if (leaf instanceof Container) { //recursive call if Container
            Container node = (Container) leaf;
            setAllComponents(node.getComponents(), myMethod);
        } //end if node
        myMethod(leaf);
    } //end looping through components
}
Run Code Online (Sandbox Code Playgroud)

调用如:

setAllComponents(this.getComponents(), changeColor());
setAllComponents(this.getComponents(), changeSize());
Run Code Online (Sandbox Code Playgroud)

java interface

254
推荐指数
10
解决办法
28万
查看次数

不能引用在不同方法中定义的内部类中的非final变量

编辑:我需要更改几个变量的值,因为它们通过计时器运行几次.我需要通过计时器每次迭代不断更新值.我无法将值设置为final,因为这会阻止我更新值,但是我收到了我在下面的初始问题中描述的错误:

我以前写过以下内容:

我收到错误"不能引用在不同方法中定义的内部类中的非final变量".

这种情况发生在双重调用价格和价格调用priceObject上.你知道我为什么会遇到这个问题.我不明白为什么我需要最后的声明.此外,如果你能看到我想要做的是什么,我该怎么做才能解决这个问题.

public static void main(String args[]) {

    int period = 2000;
    int delay = 2000;

    double lastPrice = 0;
    Price priceObject = new Price();
    double price = 0;

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            price = priceObject.getNextPrice(lastPrice);
            System.out.println();
            lastPrice = price;
        }
    }, delay, period);
}
Run Code Online (Sandbox Code Playgroud)

java methods final declaration

244
推荐指数
6
解决办法
19万
查看次数

将函数作为参数传递给java

我熟悉Android框架和Java,并希望创建一个通用的"NetworkHelper"类,它可以处理大多数网络代码,使我能够从中调用网页.

我按照developer.android.com上的这篇文章来创建我的网络类:http://developer.android.com/training/basics/network-ops/connecting.html

码:

package com.example.androidapp;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;



/**
 * @author tuomas
 * This class provides basic helper functions and features for network communication.
 */


public class NetworkHelper 
{
private Context mContext;


public NetworkHelper(Context mContext)
{
    //get context
    this.mContext = mContext;
}


/**
 * Checks if the network connection is available.
 */
public boolean checkConnection()
{ …
Run Code Online (Sandbox Code Playgroud)

java parameters android interface function

63
推荐指数
5
解决办法
8万
查看次数

Android 什么时候用 AsyncTask 什么时候用 Thread

何时使用AsyncTask和何时使用Thread两者都在后台工作,并且都可以UI Thread通过某种机制操纵控件。

multithreading android android-asynctask

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

Java - 如何将方法作为参数传递?

我有一个重复使用的代码节.这是两个例子:

public java.sql.Struct createStruct(String typeName, Object[] attributes) {
  String searchPath = getSearchPath();
  String name = setSearchPathToSchema(typeName);
  Struct ret = delegate().createStruct(name.toLowerCase(), attributes);
  setSearchPath(searchPath);
  return ret;
}

public java.sql.Array createArray(String typeName, Object[] elements) {
  String searchPath = getSearchPath();
  String name = setSearchPathToSchema(typeName);
  Array ret = delegate().createArray(name.toLowerCase(), elements);
  setSearchPath(searchPath);
  return ret;
}
Run Code Online (Sandbox Code Playgroud)

您可以看到这两种方法具有一般形式:

public <T> createXXX(String typeName, Object[] objects) {
  String searchPath = getSearchPath();
  String name = setSearchPathToSchema(typeName);
  T ret = delegate().createXXX(name.toLowerCase(), objects);
  setSearchPath(searchPath);
  return ret;
}
Run Code Online (Sandbox Code Playgroud)

具有共同签名但返回类型不同T的某些函数集的返回类型在哪里createXXX.

我非常确定如何在Javascript,F#,C#,Scala或任何其他语言中执行此操作. …

java lambda

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