public class A{
private static int[] x = {0,0,0,0};
private static int justX = 0;
public A(){}
public static int[] getX(){return x;}
public static int getJustX(){ return justX;}
}
,第二堂课是
public class B extends . . .{
private int[] x;
private justX;
public B(){
x = A.getX();
justX = A.getJustX();
Log.d("X: ", Arrays.toString(x)); //X: {0,0,0,0}
Log.d("JUST X: ", String.value(justX)); //JUST X: 0;
}
private void onTouch(){
x[0] = 1;
x[1] = 1;
x[2] = 1;
x[3] = 1;
justX = 1;
Log.d("X: ",Arrays.toString(A.getX())); //X: {1,1,1,1}
Log.d("JUST X: ", String.value(A.getJustX())); //JUST X: 0;
}
}
来自A类的数组变量已更改,但整数变量未更改,为什么会发生?
答案 0 :(得分:2)
您遇到的是Java始终是原始类型的值传递(如int
),但对于对象类型,它有点复杂(并且数组是对象)。具体来说,会发生的事情是,使用引用类型分配变量会更改它引用的对象,但不会创建新对象。分配给基元变量只会改变该变量的值。
让我们一起来看看。
public class B extends . . .{
private int[] x;
private justX;
public B(){
x = A.getX();
此时,您已将B.x
引用到A.x
引用的同一数组的引用中。所以它们都是相同数据结构的名称。
justX = A.getJustX();
此处justX
只获得了A.justX
的值(0)。
Log.d("X: ", Arrays.toString(x)); //X: {0,0,0,0}
Log.d("JUST X: ", String.value(justX)); //JUST X: 0;
}
private void onTouch(){
x[0] = 1;
x[1] = 1;
x[2] = 1;
x[3] = 1;
在这里,您已到达B.x
内部,并更改了其中部分的值。但它与A.x
也引用的对象相同,因此当您查看A.x
时,您会看到相同的更改。
justX = 1;
在这里,你只需要B.justX
并给它一个新值。
Log.d("X: ",Arrays.toString(A.getX())); //X: {1,1,1,1}
Log.d("JUST X: ", String.value(A.getJustX())); //JUST X: 0;
}
}
希望这能解释为什么你得到了你得到的结果。如果您想更改A.justX
,则需要明确更改B.x
。如果您希望能够在不更改A.x
的情况下更改.clone()
,则需要显式创建新对象(例如,使用mWebView.setWebViewClient(new CustomWebViewClient());
//...
private class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startsWith("http")) return false;//open web links as usual
//try to find browse activity to handle uri
Uri parsedUri = Uri.parse(url);
PackageManager packageManager = getActivity().getPackageManager();
Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);
if (browseIntent.resolveActivity(packageManager) != null) {
getActivity().startActivity(browseIntent);
return true;
}
//if not activity found, try to parse intent://
if (url.startsWith("intent:")) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
getActivity().startActivity(intent);
return true;
}
//try to find fallback url
String fallbackUrl = intent.getStringExtra("browser_fallback_url");
if (fallbackUrl != null) {
webView.loadUrl(fallbackUrl);
return true;
}
//invite to install
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(
Uri.parse("market://details?id=" + intent.getPackage()));
if (marketIntent.resolveActivity(packageManager) != null) {
getActivity().startActivity(marketIntent);
return true;
}
} catch (URISyntaxException e) {
//not an intent uri
}
}
return true;//do nothing in other cases
}
}
)。
答案 1 :(得分:0)
问题是您正在更改阵列的副本。完成更改数组和justX之后,必须再次设置它。
public class A{
private static int[] x = {0,0,0,0};
private static int justX = 0;
public A(){}
public static int[] getX(){return x;}
public static int getJustX(){ return justX;}
public static void setX(int[] a){x = a;};
public static void setJustX(int j){justX = j;}
}
和
public class B extends . . .{
private int[] x = A.getX();
private justX = A.get;
public B(){
x = A.getX();
justX = A.getJustX();
Log.d("X: ", Arrays.toString(x)); //X: {0,0,0,0}
Log.d("JUST X: ", String.value(justX)); //JUST X: 0;
}
private void onTouch(){
x[0] = 1;
x[1] = 1;
x[2] = 1;
x[3] = 1;
justX = 1;
A.setX(x);
A.setJustX(justX);
Log.d("X: ",Arrays.toString(A.getX())); //X: {1,1,1,1}
Log.d("JUST X: ", String.value(A.getJustX())); //JUST X: 0;
}
}