2017-07-25 69 views
1

我的老板最近告诉我,我在我的应用程序中使用类继承。有一种方法我经常使用,但.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”或任何活动。我想知道是否有更高效的方法来实现这一点,可能通过继承或在我的其他类中调用这些方法,而不必复制/粘贴所有这些代码。

+0

我检举此问题作为重构求实,低质量等 – LazerBanana

+0

没有你的代码的清醒的认识,我的建议是makke类以一个抽象的超类,然后所有的子类可以扩展类。 – Lino

+0

[继承和多态之间的主要区别是什么?](https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism) –

回答

1

您可以创建BaseActivity(为您的活动的父类)和扩展此类在您的所有活动,然后创建一个方法与开关等情况,并把它在你的所有onNavigationItemSelected方法

+0

我对你的意思有点困惑通过“,然后用switch case创建一个方法,并在所有onNavigationItemSelected方法中调用它。”我是否在BaseActivity中创建开关方法,然后根据用户所处的活动进行调用? – Mairead

+0

使用switch case在BaseActivity中创建一个方法,并从所有onNavigationItemSelected方法中调用该方法。 –

+0

啊,这很有道理。谢谢! – Mairead

1

因为它是一个完整的类,你想要继承给Aj 27的答案考虑。您可以将这些方法放在另一个类中并从中扩展,因此可以访问其所有方法,而无需始终复制和粘贴代码。

Read about inheritance here

另外,如果你的活动是非常简单,彼此没有太多的不同在功能方面。你可以考虑碎片。您可以使用托管导航栏的一个MainActivity来创建多个片段。

Read about fragments here

相关问题