通用集合和继承 [英] Generic collections and inheritance

查看:52
本文介绍了通用集合和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个基类,它包含一个通用列表,需要由基类及其子类访问

。 />
对此最好的解决办法是什么?


我对仿制药相当新,但我知道这个事实

如果你有一个继承自A的B类,然后是

List< Bdoes NOT不继承自List< A>。所以我理解

为什么下面的例子没有编译,但我没有

看看如何解决上述问题...例如,

我需要能够从下面的LeaseContract类返回BindingList< LeaseContractLine>



//样本:

使用系统;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Windows.Forms;


命名空间GenericInheritanceTest

{

公共部分类Form1:表格

{

/ /一个只有一个DataGridView的简单表单:

public Form1()

{

InitializeComponent();

}


private void Form1_Load(object sender,EventArgs e)

{

LeaseContract leaseContract = new LeaseContract();

//在DataGridView中显示合约行:

uxLeaseContractGrid.DataSource = leaseContract.GetLines();

}

}


公共类合同

{

protected BindingList< ContractLine_lines = new

BindingList< ContractLine>();

public Contract(){}

//

//提供功能的方法适用于所有类型的合同......

//

}


公共类LeaseContract:合同

{

public LeaseContract(){}

public BindingList< LeaseContractLineGetLines()

{

//无法编译(因为演员表无效):

return(BindingList< LeaseContractLine>)_行;

}

//

//特定的LeaseContract方法等

//

}


public class ContractLine

{

public ContractLine(){}

}

公共类LeaseContractLine:ContractLine

{

public LeaseContractLine(){}

}

}

任何帮助都会有很大帮助!


谢谢,

Lars

解决方案

我会尝试使合约本身成为通用类:

这样的东西:


公共类合同< Twhere T:ContractLine

{

protected BindingList< T_lines = new BindingList< T>();

}


公共类LeaseContract:Contract< LeaseContractLine>

{

....

}

-

Adam Clauss


" Lars" < no ***** @ dev.nullwrote in message

news:zK ************************* *****@megapath.net ...





我有一个持有通用的基类需要

的列表才能被基类及其子类访问。

最佳解决方案是什么?


我对仿制药相当新,但我知道这个事实

如果你有一个B类继承自A,那么

List< Bdoes NOT inherit来自List< A>。所以我理解

为什么下面的例子没有编译,但我没有

看看如何解决上述问题...例如,

我需要能够从下面的LeaseContract类返回一个BindingList< LeaseContractLine>




//样本:

使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Windows.Forms;


命名空间GenericInheritanceTest

{

公共部分类Form1:表格

{

//一个只有一个DataGridView的简单表单:

public Form1()

{

InitializeComponent();

}


private void Form1_Load(对象发件人,EventArgs e)

{

LeaseContract leaseContract = new LeaseContract();

//显示DataGridV中的合同行iew:

uxLeaseContractGrid.DataSource = leaseContract.GetLines();

}

}


公共类合同

{

protected BindingList< ContractLine_lines = new

BindingList< ContractLine>();

public合同(){}

//

//为所有类型的合同提供功能的方法......

//

}


公共类LeaseContract:合同

{

public LeaseContract(){}


public BindingList< LeaseContractLineGetLines()

{

//无法编译(因为演员表无效):

return(BindingList< LeaseContractLine>)_ lines;

}

//

//特定的LeaseContract方法等

//

}


public c ContractLine

{

public ContractLine(){}

}


公共类LeaseContractLine: ContractLine

{

public LeaseContractLine(){}

}

}


任何帮助都会有很大的帮助!


谢谢,

Lars



" Adam Clauss" < ca ***** @ tamu.eduwrote in message

news:12 ************* @ corp.supernews.com ...


>我会尝试使合约本身成为通用类:

这样的东西:


public class Contract< Twhere T:ContractLine

{

protected BindingList< T_lines = new BindingList< T>();

}


公共类LeaseContract:合约< LeaseContractLine>

{

...

}



感谢您的回复 - 这似乎解决了这个问题。当

试图在实际应用程序中实现这个时,我来了另一个问题
但是:ContractLine类持有

a对父母的引用(合同类)。尝试分配给此引用的
在下面的AddLine()中给出了编译错误:

无法将类型''Contract< T>''转换为''Contract< ContractLine>''"

(缩写信息)。


这是更新的示例代码:


使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Windows.Forms;


命名空间GenericInheritanceTst

{

公共部分类Form2:表格

{

public Form2()

{

InitializeComponent();

}

}

//合同& ContractLine类:

公共类合同< Twhere T:ContractLine,new()

{

protected BindingList< T_lines = new BindingList< T> ();

公共合同(){}


public void AddLine()

{

T contractLine = new T();

//不能编译(不能将类型''合同< T>''转换为

''合同< ; ContractLine>''"):

contractLine.Parent = this;

_lines.Add(contractLine);

}

}


公共类ContractLine

{

公共合同< ContractLineParent;

public ContractLine(){}

}


// LeaseContract& LeaseContractLine类:

公共类LeaseContract:Contract< LeaseContractLine>

{

public LeaseContract(){}


public BindingList< LeaseContractLineGetLines()

{

return _lines;

}

}


公共类LeaseContractLine:ContractLine

{

public LeaseContractLine(){}

}

}


我缺少什么?


谢谢,

Lars


"拉斯" < no ***** @ dev.nullwrote in message

news:b9 ************************* *****@megapath.net ...


感谢您的回复 - 这似乎解决了这个问题。当

试图在实际应用程序中实现这个时,我来了另一个问题
但是:ContractLine类持有

a对父母的引用(合同类)。尝试分配给此引用的
在下面的AddLine()中给出了编译错误:

无法将类型''Contract< T>''转换为''Contract< ContractLine>''"

(缩写信息)。



嘿......其他人正在尝试做一些非常相似的事情。

见Kris Jennings的帖子(日期为07/21) / 2006 8:51 pm) - 更具体地说

见Barry Kelly的回复(9:28 pm)。


无论如何,ContractLine可能也需要是通用的。


命名空间GenericInheritanceTst

{

公共部分类Form2:表格

{

public Form2()

{

InitializeComponent();

}

}


//合同& ContractLine类:

公共类合同< Twhere T:ContractLine< T>,new()

{

protected BindingList< T_lines = new BindingList< ; T>();

public Contract(){}


public void AddLine()

{

T contractLine = new T();

contractLine.Parent = this;

_lines.Add(contractLine);

}

}


公共类ContractLine< LineBasewhere LineBase:

ContractLine< LineBase>,new()

{

公共合同< ContractLineParent;

公共ContractLine(){}

}


// LeaseContract& LeaseContractLine类:

公共类LeaseContract:Contract< LeaseContractLine>

{

public LeaseContract(){}


public BindingList< LeaseContractLineGetLines()

{

return _lines;

}

}


公共类LeaseContractLine:ContractLine< LeaseContractLine>

{

public LeaseContractLine(){}

}

}

泛型非常好...通用代码。但是他们很难有时候把你的思绪包裹起来。

-

Adam Clauss


Hi,

I have a base class holding a generic list that needs
to be accessed by both the base class and its subclasses.
What is the best solution to this?

I am fairly new to generics, but I am aware of that fact
that if you have a class B, that inherits from A, then
List<Bdoes NOT inherit from List<A>. So I understand
why the example below does not compile, but I fail to
see how to solve the problem stated above... For example,
I need to be able to return a BindingList<LeaseContractLine>
from the LeaseContract class below.
// Sample:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace GenericInheritanceTest
{
public partial class Form1 : Form
{
// A simple form with only a DataGridView on it:
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
LeaseContract leaseContract = new LeaseContract();
// Display the contract lines in a DataGridView:
uxLeaseContractGrid.DataSource = leaseContract.GetLines();
}
}

public class Contract
{
protected BindingList<ContractLine_lines = new
BindingList<ContractLine>();
public Contract() { }
//
// methods providing functionality for all types of contracts...
//
}

public class LeaseContract : Contract
{
public LeaseContract() { }

public BindingList<LeaseContractLineGetLines()
{
// Will not compile (since the cast is not valid):
return (BindingList<LeaseContractLine>)_lines;
}
//
// specific LeaseContract methods, etc.
//
}

public class ContractLine
{
public ContractLine() { }
}
public class LeaseContractLine : ContractLine
{
public LeaseContractLine() { }
}
}
Any help would be greatly appreicated!

Thanks,
Lars

解决方案

I would try making Contract itself a generic class:
Something like this:

public class Contract<Twhere T : ContractLine
{
protected BindingList<T_lines = new BindingList<T>();
}

public class LeaseContract : Contract<LeaseContractLine>
{
....
}
--
Adam Clauss

"Lars" <no*****@dev.nullwrote in message
news:zK******************************@megapath.net ...

Hi,

I have a base class holding a generic list that needs
to be accessed by both the base class and its subclasses.
What is the best solution to this?

I am fairly new to generics, but I am aware of that fact
that if you have a class B, that inherits from A, then
List<Bdoes NOT inherit from List<A>. So I understand
why the example below does not compile, but I fail to
see how to solve the problem stated above... For example,
I need to be able to return a BindingList<LeaseContractLine>
from the LeaseContract class below.
// Sample:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace GenericInheritanceTest
{
public partial class Form1 : Form
{
// A simple form with only a DataGridView on it:
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
LeaseContract leaseContract = new LeaseContract();
// Display the contract lines in a DataGridView:
uxLeaseContractGrid.DataSource = leaseContract.GetLines();
}
}

public class Contract
{
protected BindingList<ContractLine_lines = new
BindingList<ContractLine>();
public Contract() { }
//
// methods providing functionality for all types of contracts...
//
}

public class LeaseContract : Contract
{
public LeaseContract() { }

public BindingList<LeaseContractLineGetLines()
{
// Will not compile (since the cast is not valid):
return (BindingList<LeaseContractLine>)_lines;
}
//
// specific LeaseContract methods, etc.
//
}

public class ContractLine
{
public ContractLine() { }
}
public class LeaseContractLine : ContractLine
{
public LeaseContractLine() { }
}
}
Any help would be greatly appreicated!

Thanks,
Lars



"Adam Clauss" <ca*****@tamu.eduwrote in message
news:12*************@corp.supernews.com...

>I would try making Contract itself a generic class:
Something like this:

public class Contract<Twhere T : ContractLine
{
protected BindingList<T_lines = new BindingList<T>();
}

public class LeaseContract : Contract<LeaseContractLine>
{
...
}

Thanks for the reply - that seems to solve that problem. When
trying to implement this in the actual application, I come
across another problem, though: The ContractLine class holds
a reference to the parent (the Contract class). Trying to
assign to this reference gives a compile error in AddLine() below:
"Cannot convert type ''Contract<T>'' to ''Contract<ContractLine>''"
(abbreviated message).

Here is the updated sample code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace GenericInheritanceTst
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}

// Contract & ContractLine class:
public class Contract<Twhere T : ContractLine, new()
{
protected BindingList<T_lines = new BindingList<T>();
public Contract() { }

public void AddLine()
{
T contractLine = new T();
// Won''t compile ("Cannot convert type ''Contract<T>'' to
''Contract<ContractLine>''"):
contractLine.Parent = this;
_lines.Add(contractLine);
}
}

public class ContractLine
{
public Contract<ContractLineParent;
public ContractLine() { }
}

// LeaseContract & LeaseContractLine class:
public class LeaseContract : Contract<LeaseContractLine>
{
public LeaseContract() { }

public BindingList<LeaseContractLineGetLines()
{
return _lines;
}
}

public class LeaseContractLine : ContractLine
{
public LeaseContractLine() { }
}
}

What am I missing?

Thanks,
Lars


"Lars" <no*****@dev.nullwrote in message
news:b9******************************@megapath.net ...

Thanks for the reply - that seems to solve that problem. When
trying to implement this in the actual application, I come
across another problem, though: The ContractLine class holds
a reference to the parent (the Contract class). Trying to
assign to this reference gives a compile error in AddLine() below:
"Cannot convert type ''Contract<T>'' to ''Contract<ContractLine>''"
(abbreviated message).

Heh... incidentally someone else is trying to do something very similar.
See the post by Kris Jennings (dated 07/21/2006 8:51pm) - more specifically
see the reply to that by Barry Kelly (9:28pm).

In any event, the ContractLine probably also needs to be generic.

namespace GenericInheritanceTst
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}

// Contract & ContractLine class:
public class Contract<Twhere T : ContractLine<T>, new()
{
protected BindingList<T_lines = new BindingList<T>();
public Contract() { }

public void AddLine()
{
T contractLine = new T();
contractLine.Parent = this;
_lines.Add(contractLine);
}
}

public class ContractLine<LineBasewhere LineBase :
ContractLine<LineBase>, new()
{
public Contract<ContractLineParent;
public ContractLine() { }
}

// LeaseContract & LeaseContractLine class:
public class LeaseContract : Contract<LeaseContractLine>
{
public LeaseContract() { }

public BindingList<LeaseContractLineGetLines()
{
return _lines;
}
}

public class LeaseContractLine : ContractLine<LeaseContractLine>
{
public LeaseContractLine() { }
}
}
Generics make for very good... generic code. But they can be difficult to
wrap your mind around at times.
--
Adam Clauss


这篇关于通用集合和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆