2012-07-24 88 views
0

部分类我有这个类的父类:如何继承存储过程调用

public partial class GetStuffResult 
{ 

    private int _Id; 

    private string _Name; 

    public GetStuffResult() 
    { 
    } 

    [Column(Storage="_Id", DbType="INT NOT NULL")] 
    public int Id 
    { 
     get 
     { 
      return this._Id; 
     } 
     set 
     { 
      if ((this._Id != value)) 
      { 
       this._Id = value; 
      } 
     } 
    } 

    [Column(Storage="_Name", DbType="NVarChar(100)")] 
    public string Name 
    { 
     get 
     { 
      return this._Name; 
     } 
     set 
     { 
      if ((this._Name != value)) 
      { 
       this._Name = value; 
      } 
     } 
    } 
} 

这是有一个额外的方法的异常相同方法的基类:

public partial class GetStuffResult1 
{ 
    private int _Score; 

    private int _Id; 

    private string _Name; 

    public GetStuffResult1() 
    { 
    } 

    [Column(Storage="_Score", DbType="INT NOT NULL")] 
    public int Id 
    { 
     get 
     { 
      return this._Score; 
     } 
     set 
     { 
      if ((this._Score != value)) 
      { 
       this._Score = value; 
      } 
     } 
    } 

    [Column(Storage="_Id", DbType="INT NOT NULL")] 
    public int Id 
    { 
     get 
     { 
      return this._Id; 
     } 
     set 
     { 
      if ((this._Id != value)) 
      { 
       this._Id = value; 
      } 
     } 
    } 

    [Column(Storage="_Name", DbType="NVarChar(100)")] 
    public string Name 
    { 
     get 
     { 
      return this._Name; 
     } 
     set 
     { 
      if ((this._Name != value)) 
      { 
       this._Name = value; 
      } 
     } 
    } 
} 

我之前做过继承,但我完全困惑它将如何在这种情况下工作?我如何继承GetStuffResult,以便我可以使用它的两个方法,而不必在GetStuffResult1中将相同的代码粘贴两次。 如果有人可以举例说明代码,我将会很感激,因为我是.NET 3.5的新手,并且仍然在努力学习它。

回答

1

我不知道我是否正确理解你的问题。 (您当前的GetStuffResult1代码不应该编译你有两次定义Id属性。)如果你正在寻找从GetStuffResult继承那么这会做(见Inheritance):

public partial class GetStuffResult1 : GetStuffResult 
    { 
     private int _Score; 

     public GetStuffResult1() 
     { 
     } 

     [Column(Storage = "_Score", DbType = "INT NOT NULL")] 
     public int Id 
     { 
      get 
      { 
       return this._Score; 
      } 
      set 
      { 
       if ((this._Score != value)) 
       { 
        this._Score = value; 
       } 
      } 
     } 


    } 

注意,我已删除来自子类的_Id_Name。然而,这会给你警告:

GetStuffResult1.Id”隐藏继承成员 'ThreadConsoleApp.GetStuffResult.Id'。如果想要隐藏 ,请使用新关键字。

如果您对使用部分类感到困惑,并且您可能需要多个源文件中的单个类,那么我正在考虑您的问题的第二件事。在这种情况下,您可以使用partial关键字。如果是这种情况,你不需要继承,那么你需要为这个类使用一个单一的名称。例如GetStuffResult。在这种特殊情况下你GetStuffResult1将变为:

public partial class GetStuffResult 
    { 
     private int _Score; 

     public GetStuffResult1() 
     { 
     } 

     [Column(Storage = "_Score", DbType = "INT NOT NULL")] 
     public int Id 
     { 
      get 
      { 
       return this._Score; 
      } 
      set 
      { 
       if ((this._Score != value)) 
       { 
        this._Score = value; 
       } 
      } 
     } 


    } 

这将是与具有与所有的组合性质的单一类。

编辑:

要访问子类的基类的属性,你可以使用base关键字。

base.Id = 0; 
    base.Name = "SomeName"; 

要访问GetStuffResult1对象中的基类属性,请参阅以下示例。

GetStuffResult1 gsr1 = new GetStuffResult1(); 
    gsr1.Id = 0; 
    gsr1.Name = "SomeName"; 

这里gsr1.Name是从基类,您可以在碱或子类Id使用不同的名称,以便它可以更清晰。

+0

非常感谢回答,我还有一个问题。因此,在子类中,我们将删除_Id和_Name。我怎么称呼这些? – NoviceMe 2012-07-25 03:55:46

+0

@NoviceMe,检查编辑答案 – Habib 2012-07-25 04:02:01

+0

非常感谢。我认为这有帮助,我在相同的条件下思考,但这是一个linq查询,我从来没有使用过,它是完全抛弃我。让我试试这个。再次感谢一吨! – NoviceMe 2012-07-25 04:07:22