简体   繁体   English

使用像这样的继承是一个坏主意

[英]Is using inheritance like this a bad idea

Hello I'm inexperienced in programming and learning slowly by making a project for android. 您好,我在编程和学习方面经验不足,通过制作Android项目。

The problem is I'm using a lot of the same code in my activities. 问题是我在我的活动中使用了很多相同的代码。 Is it a bad idea to share code using inheritance. 使用继承共享代码是一个坏主意。

example: 例:

Class Main extends Activity
       //shared code

           _
Class a     |
Class b     | - extends Main
Class c    _|

my usage example: 我的用法示例:

public class Global extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

public void toastMessage(String tMsg, int tLength) {
    Toast.makeText(getApplicationContext(), tMsg, tLength).show();
}

public void nextActivity(Class<?> classId, ImageButton btnId) {
    AlphaAnimation alpha;
    Vibrator shake = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    if (btnId!= null) {
        alpha = new AlphaAnimation(0.5F, 1.0F);
        alpha.setDuration(50);
        alpha.setFillAfter(false);
        btnId.startAnimation(alpha);
    }
    shake.vibrate(50);
    Intent goToNextActivity = new Intent(getApplicationContext(), classId);
    startActivity(goToNextActivity);
}

Class a,b,c could extend Global and use: a,b,c类可以扩展Global并使用:

  nextActivity(a.class, btnA);
  toastMessage("example", Toast.LENGTH_SHORT);

In general is not a good idea use inheritance only for code reuse. 一般来说,仅仅为代码重用使用继承并不是一个好主意。 Inheritance use the "is a" paradigm. 继承使用“是一种”范式。 Look if is better for you to use Composition. 看看你是否更好地使用Composition。 Follow the advice of Matthijs, post always your code. 遵循Matthijs的建议,始终发布您的代码。

Inheritance is used to say that a class belongs to some "category" for example: 继承用于表示类属于某个“类别”,例如:

a class named "goblin" extends the class "enemy". 一个名为“goblin”的类扩展了类“敌人”。 Inside "enemy" there is simple code that applies to all enemys (walking, ai, attacking etc), inside "goblin" class there is code like the graphics, maybe some dialog, animation or stuff that are not shared to all enemies. 在“敌人”里面有一个简单的代码适用于所有的敌人(步行,攻击,攻击等),在“地精”类里面有代码,如图形,也许是一些对话,动画或不与所有敌人共享的东西。

There are cases where this is very useful like controlling all enemies, despite some of them are actually goblins or orcs or whatever. 有些情况下,这对控制所有敌人非常有用,尽管其中一些实际上是地精或兽人或其他什么。 About code re-use you can use methods to do it, keep in mind that your code must be easy to read (even if it's just for you) so don't put all steps inside one class, for example (it's going to be a video game example again :P ) 关于代码重用,您可以使用方法来执行此操作,请记住您的代码必须易于阅读(即使它只适合您)所以不要将所有步骤放在一个类中,例如(它将会是一个视频游戏示例:P)

while(game == true)
{
    getInput();

    moveHero();

    moveEnemies();

    applyGravity();

    drawFrame();
}

Even if there are cases like "moveHero" and "moveEnemies" are going to be applied one after another almost always, it's not a good idea to group them together, so you can easily tell what's going on in your program. 即使像“moveHero”和“moveEnemies”这样的情况几乎总是一个接一个地应用,将它们组合在一起并不是一个好主意,这样你就可以轻松地告诉程序中发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM