public class MainActivity extends Activity {
public Button button;
private MyApp myApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myApp = (MyApp) getApplication();
myApp.setName("jack");
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
}
public class MyApp extends Application {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setName("张三");
}
}
public class OtherActivity extends Activity {
private MyApp myApp;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView) this.findViewById(R.id.msg);
myApp = (MyApp) getApplication();
textView.setText("--appname-->>"+myApp.getName());
}
}<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity">
</activity>
</application>
本文深入探讨了Android应用开发中的对象继承与多态概念,通过实例展示了如何在实际项目中运用这些高级特性来提高代码的复用性和可维护性。
1万+

被折叠的 条评论
为什么被折叠?



