2011-12-19 121 views
1

我把我的变量为一个基类是这样的:访问方法

public class BaseController : Controller 
{ 
    protected BaseViewModel vm { get; set; } 
    protected IAccountService _account; 
    protected IDataSourceService _dataSource; 
    protected IPackageService _package; 

每个控制器目前执行以下操作:

private void InitializeServices(string ds, string ac = null, string pr = null, string pa = null, string co = null) { 

     _dataSource = new DataSourceService(); 
     _account = new AccountService(ds); 
     _product = new ProductService(ds); 
     _package = new PackageService(ds); 

但有什么办法我现在可以改变它,将InitializeService方法放入基类中,然后从派生类中调用它。如果可能的话,我将如何从派生类中调用该方法,并且一旦它位于基类内部时,该类的修饰符是什么?这也会受到保护吗?

回答

2

只需将该方法的代码移至基类,并使用修饰符protected即可。然后你可以从你的派生类调用它,就好像它是该类的成员(它是通过继承)。

+0

谢谢安德鲁。标记为已接受。 – 2011-12-19 04:49:07