我的老板最近告诉我,我在我的应用程序中使用类继承。有一种方法我经常使用,但.this
类已更改。我可以创建一个基类来继承这些信息吗?
这里是基本代码我使用:
public class CURRENTCLASS extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_CURRENTCLASS);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CURRENTCLASS.this, ActivityMenu.class);
startActivity(intent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.about) { //goes to Our company page
Intent intent = new Intent(CURRENTCLASS.this, ActivityAbout.class);
startActivity(intent);
}
else if (id == R.id.news) { // goes to News and events page
Intent intent = new Intent(CURRENTCLASS.this, ActivityNewsEvents.class);
startActivity(intent);
}
else if (id == R.id.serve) { // markets we serve
Intent intent = new Intent(CURRENTCLASS.this, ActivityMarkets.class);
startActivity(intent);
}
else if (id == R.id.seta) { //seta and aquisition support
Intent intent = new Intent(CURRENTCLASS.this, ActivitySETA.class);
startActivity(intent);
}
else if (id == R.id.software) { //software and systems dev
Intent intent = new Intent(CURRENTCLASS.this, ActivitySoftware.class);
startActivity(intent);
}
else if (id == R.id.training) { //intelligence analytics and training
Intent intent = new Intent(CURRENTCLASS.this, ActivityAnalytics.class);
startActivity(intent);
}
else if (id == R.id.QACSTUFF) { //QAC
Intent intent = new Intent(CURRENTCLASS.this, ActivityQAC.class);
startActivity(intent);
}
else if (id == R.id.careers) { //Careers
Intent intent = new Intent(CURRENTCLASS.this, ActivityCareers.class);
startActivity(intent);
}
else if (id == R.id.contactUs) { //Contact us
Intent intent = new Intent(CURRENTCLASS.this, ActivityContact.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
我在我的大部分类的利用这一点,我只是改变CURRENTCLASS
到任何类我将其复制到。所以,我的问题是,我可以将它作为我继承的类吗?因此,我可以使用这些方法而无需将它们复制/粘贴到每个类中?如果是这样,有没有人有资源/建议,我会怎么做?
好的,澄清。 我在我的android应用程序中有15个以上的类,它们都具有相同的基本代码,但略有更改。在代码中,无论它说什么“CURRENTCLASS”,我都会将其改为“ActivityMain”或“ActivityDevelopment”或任何活动。我想知道是否有更高效的方法来实现这一点,可能通过继承或在我的其他类中调用这些方法,而不必复制/粘贴所有这些代码。
我检举此问题作为重构求实,低质量等 – LazerBanana
没有你的代码的清醒的认识,我的建议是makke类以一个抽象的超类,然后所有的子类可以扩展类。 – Lino
[继承和多态之间的主要区别是什么?](https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism) –