內部類呼叫外部類屬性(new 物件,屬性重新初始化)
阿新 • • 發佈:2018-12-26
package com.example.myapplication; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.text.SimpleDateFormat; import java.util.Date; public class TestActivity extends Activity { int a=0; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.test); Button test=(Button) findViewById(R.id.neibulei); test.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { a=5; new hhh().getnum();//呼叫改程式碼後,log打印出的a=0;因為重新 new 了一個TestActivity物件,引數均被初始化。 } }); } public static class hhh{ public void getnum(){ Log.i("czk","a="+new TestActivity().a); } } }