java - java中如何修改引用变量的值

标签 java arguments

A a = new A(); 
method(A c){  
    {  
     //asynchronous network request   
      c = new A();   
    //how can i assign c to A,don't use final modifier orstatic modifier   
    } 
}

如上所述,由于某些原因,我想将c的值赋给a。而且我们不能直接赋值:a = c。简而言之,我希望a和c指向同一个对象或地址。任何人都可以帮助我。提前致谢。

我所有的代码都在这里:

public class LoadResource {
    public static String tag = "LoadResource:";
    public static int nRightOption;
    public static GameInfo gameInfo;
    public static Pixmap pixFont;// font
    public static Pixmap pixOption[] = new Pixmap[4];// Option pixmap
    public static Pixmap imgArray[]=new Pixmap[3];// Detail Pixmap
    public static Pixmap imgArray_2[] = new Pixmap[2];// 
    public static Pixmap historyImg[];// history pixmap

    public LoadResource() {
        // TODO Auto-generated constructor stub

    }

    /*
     * Loadgame GameInfo
     */
    public void startupLoad() {
        IGameInfo gameInfo = ServiceGenerator.createService(IGameInfo.class);
        Call<GameInfo> call = gameInfo.getGameInfo("15", "84889382", String.valueOf(LevelInfo.currentLevel),
                String.valueOf(LevelInfo.currentSubject));
         Log.e(tag, tag+"startupLoad");
        // request
        try {
            call.enqueue(new Callback<GameInfo>() {

                @Override
                public void onFailure(Throwable arg0) {
                    // TODO Auto-generated method stub
                    Log.e(tag, "network access failure" + arg0.toString());
                }

                public void onResponse(Response<GameInfo> arg0, Retrofit arg1) {
                    // TODO Auto-generated method stub
                    // result
                    if (arg0.isSuccess()) {
                        LoadResource.gameInfo = arg0.body();
                        Log.e(tag, LoadResource.gameInfo.toString());
                        if (LoadResource.gameInfo != null&&LoadResource.gameInfo.getDataArray()!=null) {
                            Log.e(tag, tag + LoadResource.gameInfo.toString());
                            // loadinfo
                            nRightOption = LoadResource.gameInfo.getDataArray().getCurrenSelectImg();
                            // start thread and load pixmap
                            Log.e(tag,tag+"startupLoadPixmap");
                            getGamePixmapData();
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
    }

    /*
     * start four kinds of thread and load pixmap seperately
     */
    public static void getGamePixmapData() {
        // font
        String strFont = gameInfo.getDataArray().getFontUrl();
        // check if exsit in local file,if not exist get pixmap from network
        if ((pixFont = pixmapLoader.getPixmap(strFont)) == null)
            downloadfile(strFont, pixFont);
        // option
        String strOption[] = gameInfo.getDataArray().getSelectImgs().split(",");// option pixmap
        pixOption = new Pixmap[strOption.length];
        for (int i = 0; i < strOption.length; i++) {
            if ((pixOption[i] = pixmapLoader.getPixmap(strOption[i])) == null)
                downloadfile(strOption[i], pixOption[i]);
        }
        // detail pixmap
        String strDetailPixmap[] = gameInfo.getDataArray().getImgArray().split(",");
        imgArray = new Pixmap[strDetailPixmap.length];
        for (int i = 0; i < strDetailPixmap.length; i++) {
            if ((imgArray[i] = pixmapLoader.getPixmap(strDetailPixmap[i])) == null)
                downloadfile(strFont, imgArray[i]);
        }
        // history pixmap
        String strHistoryPixmap[] = gameInfo.getDataArray().getHistoryImg().split(",");
        historyImg = new Pixmap[strHistoryPixmap.length];
        for (int i = 0; i < strHistoryPixmap.length; i++) {
            if ((historyImg[i] = pixmapLoader.getPixmap(strHistoryPixmap[i])) != null)
                downloadfile(strFont, historyImg[i]);
        }
    }
    /*
     * download file and save to file and static variable @param url:String  @param pixmap:Pixmap 
     */
    public static void downloadfile(String url, Pixmap pixmap) {
        IGameInfo gameInfo = ServiceGenerator.createService(IGameInfo.class);
        Call<ResponseBody> call = gameInfo.downloadFile(url);
        call.enqueue(new LoadResource.downloadToPixmap(pixmap));
    }

    private static class downloadToPixmap implements Callback<ResponseBody> {
        private Pixmap pixLoad;

        public downloadToPixmap(Pixmap pixLoad) {
            super();
            this.pixLoad = pixLoad;
        }

        @Override
        public void onFailure(Throwable paramThrowable) {
            // TODO Auto-generated method stub
            Log.e(tag, "network access failure" + paramThrowable.toString());
        }

        @Override
        public void onResponse(Response<ResponseBody> paramResponse, Retrofit paramRetrofit) {
            // TODO Auto-generated method stub
            if (paramResponse.isSuccess()) {
                try {
                    Log.e(tag, "successLoad");          
                    final byte bytes[] = (paramResponse.body()).bytes();
                    pixLoad = new Pixmap(bytes, 0, bytes.length);
                    if(imgArray[0]!=null) Log.e(tag, tag+"loadImgArray");
                    if(pixLoad!=null) Log.e(tag, "successLoadPixmap");
                    // put in memory and file
                    InputStream inStream = paramResponse.body().byteStream();
                    pixmapLoader.putPixmapToMemory(paramResponse.raw().request().url().toString(), pixLoad);
                    pixmapLoader.putPixmapToFile(inStream, paramResponse.raw().request().url().toString());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

就像上面我的变量 pixmap 作为 downloadfile 中的参数传输一样,无法在 downloadToPixmap 中获取 new Pixmap()。那么我该如何实现它。

最佳答案

您不能通过引用传递参数,但可以使用可变包装器作为解决方法。基本思想如下:

class Wrapper<T> {
    T value;
}

void method(Wrapper<A> c) {
    c.value = new A();
}

void foo() {
    Wrapper<A> a = new Wrapper<>();
    method(a);
    // a.value is now updated
}

关于java - java中如何修改引用变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38004772/

相关文章:

java.lang.ClassNotFoundException : sun. jdbc.odbc.JdbcOdbcDriver 在 java 中不再工作。如何解决这个问题?

java - 在您的软件项目中有 "Utils"类是否很好?

java - 使用 FileUtils.copyURLToFile 设置 SSL 版本

java - 自定义对象转换器 JavaFx FXML

javascript - “leaking arguments”是谎言吗?

适合 Web 设计师初学者的 jQuery

php - 在 php 中解压一组参数

java - Nashorn - 找不到 ScriptObject 和 MyInterface 的通用类加载器

java - 通过 System.out.println();作为方法中的参数

c - main() 是否可以接受任何其他参数?