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中获得新的Pixmap(),我该怎样才能实现它。
答案 0 :(得分:1)
您不能通过引用传递参数,但可以使用可变包装器作为变通方法。这是基本的想法:
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
}
答案 1 :(得分:0)
您有两种方式:
通过方法()返回c的值如下:
A a = new A();
a = method(a); // returned value will be assigned to variable a
A method(A c){
{
//asynchronous network request
c = new A();
return c;
}
}
私人A a =新A(); // decalred为类成员变量
void method(A c) {
c = new A();
a = c;
}