Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何减少扩展不同基类的两个类中的代码重复?_C#_Oop_Xamarin.android - Fatal编程技术网

C# 如何减少扩展不同基类的两个类中的代码重复?

C# 如何减少扩展不同基类的两个类中的代码重复?,c#,oop,xamarin.android,C#,Oop,Xamarin.android,我有一个类必须扩展活动,例如 BaseActivity : Activity { } BaseListActivity : ListActivity {} // ListActivity in turn extends Activity 以及另一个必须扩展ListActivity的类,例如 BaseActivity : Activity { } BaseListActivity : ListActivity {} // ListActivity in turn extends Activi

我有一个类必须扩展
活动
,例如

BaseActivity : Activity { }
BaseListActivity : ListActivity {} // ListActivity in turn extends Activity
以及另一个必须扩展
ListActivity
的类,例如

BaseActivity : Activity { }
BaseListActivity : ListActivity {} // ListActivity in turn extends Activity
这两个类都有一些相同的方法,例如

OnCreate(Bundle bundle), OnStart(), OnStop()
在两者中使用完全相同的实现,例如

BaseActivity : Activity {

    public bool isBound = false;
    public MyServiceBinder binder;
    MyServiceConnection _myServiceConnection;
    MyReceiver _myReceiver;
    internal Intent _myServiceIntent;   

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        _myServiceIntent = new Intent(this, typeof(MyServiceCls));
        _myReceiver = new MyReceiver();

        _myServiceConnection = LastNonConfigurationInstance as MyServiceConnection;

        if (_myServiceConnection != null)
            binder = _myServiceConnection.Binder;
    }

    protected override void OnStart()
    {
        base.OnStart();

        var intentFilter = new IntentFilter(MyServiceCls.MyUpdatedAction) { Priority = (int)IntentFilterPriority.HighPriority };
        RegisterReceiver(_myReceiver, intentFilter);

        _myServiceConnection = new MyServiceConnection(this);
        Application.Context.BindService(_myServiceIntent, _myServiceConnection, Bind.AutoCreate);

        StartService(_myServiceIntent);
    }

    protected override void OnStop()
    {
        base.OnStop();

        if (isBound)
        {
            Application.Context.UnbindService(_myServiceConnection);

            isBound = false;
        }

        StopService(_myServiceIntent);

        UnregisterReceiver(_myReceiver);
    }
}

区别在于两者的用法。比如说

MyActivity : BaseActivity 
{ /* this overrides the methods and does things differently */}

最终,我的问题是我不想在
BaseActivity
BaseListActivity
类中复制相同的代码。解决这一问题的最佳方法是什么?我仍然可以拥有执行所需功能所需的覆盖,而不需要代码重复


注意:你们中的一些人可能会认为这是一个Android项目——确实如此。它是用Xamarin(又名Monodroid)构建的,这就是为什么它在C#中的原因。

将公共方法旋转为助手类的静态方法,并提供公共代码依赖的所有变量作为参数。

将公共代码提取到自己的类中,并在基类重写中调用该类的方法。你将无法摆脱它们。

问题不在于它们没有共同的基类吗?@spender:当然。我没有说他应该将公共代码提取到基类中。组合是这里的相关关键字。