铸造通用集合&遗产 [英] Casting generic collections & inheritance
问题描述
好的,我的A级定义如下:
A级
{
A(队列< B>排队){...}
}
现在,我有一个A类和B类的子类.A的子类
(SubA),更具体地说是传递给队列< SubB> ;.
class SubA
{
SubA (队列< SubB>队列)
:基础(队列)
{...}
}
乍一看这似乎是正确的,因为如果一个队列包含SubB类型的所有元素,那么根据定义那些元素也是B类。所以,
类型应该是兼容的。
然而,在编译时,我收到一个错误,说它无法转换类型。
此外,队列没有有一个Convert()方法,就像List一样。任何
如何解决这个问题的建议?
谢谢
-
Adam Clauss
OK, I have class A defined as follows:
class A
{
A(Queue<B> queue) { ... }
}
Now, I then have a subclass of both classes A and B. The subclass of A
(SubA), more specifically is passed a Queue<SubB>.
class SubA
{
SubA(Queue<SubB> queue)
: base (queue)
{ ... }
}
This, at first glance appears to be correct, because if a queue contains all
elements of type SubB, then those by definition are also of type B. So, the
types should be compatible.
However, upon compiling, I get an error saying it cannot convert the types.
Further, Queue does not have a Convert() method, as the List does. Any
suggestions on how to solve this?
Thanks
--
Adam Clauss
推荐答案
" Adam Clauss" < CA ***** @ tamu.edu> écritdansle message de news:
11 ******** *****@corp.supernews.com ...
|乍一看似乎是正确的,因为如果一个队列包含
all
| SubB类型的元素,那么根据定义,它们也是B类。所以,
|类型应该兼容。
|
|但是,在编译时,我收到错误消息,说它无法转换
类型。
|此外,Queue没有像List那样的Convert()方法。任何
|关于如何解决这个问题的建议?
如果你继承自Queue< T>,那么你就可以分配子类
的队列< T> ;,但是队列< SubB>不继承自Queue< B>,它被视为
作为一个单独的类。
泛型类的继承依赖于泛型类型,而不是参数
类型。
Joanna
-
Joanna Carter [TeamB]
顾问软件工程师
"Adam Clauss" <ca*****@tamu.edu> a écrit dans le message de news:
11*************@corp.supernews.com...
| This, at first glance appears to be correct, because if a queue contains
all
| elements of type SubB, then those by definition are also of type B. So,
the
| types should be compatible.
|
| However, upon compiling, I get an error saying it cannot convert the
types.
| Further, Queue does not have a Convert() method, as the List does. Any
| suggestions on how to solve this?
If you inherited from Queue<T>, then you would be able to assign sub-classes
of Queue<T>, but Queue<SubB> does not inherit from Queue<B>, it is regarded
as a separate class.
inheritance of generic classes relies on the generic type, not the parameter
type.
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
嗯......你会认为编译器能够做出这样的区分 -
,队列< SubB>也是一个队列< B> ;.
-
Adam Clauss
" Joanna Carter [TeamB ]" <乔**** @ not.for.spam>在消息中写道
news:eq ************** @ TK2MSFTNGP10.phx.gbl ...
Hmm... you would think the compiler would be able to make that distinction -
that a Queue<SubB> is also a Queue<B>.
--
Adam Clauss
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
" Adam Clauss" < CA ***** @ tamu.edu> écritdansle message de news:
11 ******** *****@corp.supernews.com ......
|乍一看似乎是正确的,因为如果一个队列包含
所有
| SubB类型的元素,那么根据定义,它们也是B类。所以,
|类型应该是兼容的。
|
|但是,在编译时,我收到错误消息,说它无法转换
类型。
|此外,Queue没有像List那样的Convert()方法。任何
|关于如何解决这个问题的建议?
如果你继承自Queue< T>,那么你就可以分配队列< T>的子类了,但是队列< ; SUBB>不继承自Queue< B>,它被认为是一个单独的类。
继承泛型类依赖于泛型类型,而不是
参数
类型。
Joanna
- Joanna Carter [TeamB]
顾问软件工程师
"Adam Clauss" <ca*****@tamu.edu> a écrit dans le message de news:
11*************@corp.supernews.com...
| This, at first glance appears to be correct, because if a queue contains
all
| elements of type SubB, then those by definition are also of type B. So,
the
| types should be compatible.
|
| However, upon compiling, I get an error saying it cannot convert the
types.
| Further, Queue does not have a Convert() method, as the List does. Any
| suggestions on how to solve this?
If you inherited from Queue<T>, then you would be able to assign
sub-classes
of Queue<T>, but Queue<SubB> does not inherit from Queue<B>, it is
regarded
as a separate class.
inheritance of generic classes relies on the generic type, not the
parameter
type.
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
" Adam Clauss" < CA ***** @ tamu.edu>写在
新闻:11 ************* @ corp.supernews.com:
"Adam Clauss" <ca*****@tamu.edu> wrote in
news:11*************@corp.supernews.com:
嗯...你会认为编译器能够区分 - 一个队列< SubB>也是一个队列< B>。
Hmm... you would think the compiler would be able to make that
distinction - that a Queue<SubB> is also a Queue<B>.
我一直在想这个......虽然它不能直接投,
你可以将每个项目添加到一个新的列表中..
这里任何一个聪明的人都可以解释为什么这种类型的通用
继承不是包含在语言/编译器中?
这里有一些示例代码...请特别注意关于
中途的两条评论。 (抱歉,包装不好 - 有没有更好的方法来支付
这样做?我以后只需要附上文件吗?)
使用系统;
使用System.Collections.Generic;
使用System.Text;
namespace GenericTypeInheritanceTest
{
class program
{
static void Main(string [] args)
{
列表< Person> people = new List< Person>();
Person mike = new Person(" mike");
Farmer john = new Farmer(" john",10);
Farmer bob = new Farmer(" bob",20);
people.Add(mike);
people.Add(john);
Console.WriteLine(" People:");
foreach(人物中的人物) )Console.WriteLine
(p.ToString());
List< Farmer>农民=新名单<农民>();
农民。添加(约翰);
farmer.Add(bob);
控制台。 WriteLine();
Console.WriteLine(" Farmers:");
foreach(农民农民)Console.WriteLine
(f.ToString());
//无法编译
// List< Person> otherPeople = new List< Person>
(农民);
List< Person> otherPeople = new List< Person>();
//但是这就像你期望的那样有效!
foreach(Farmer f in农民)otherPeople.Add(f);
Console.WriteLine();
Console.WriteLine(" Other People:");
foreach(其他人员)Console.WriteLine
(p.ToString());
Console.ReadLine();
}
}
公共类人员
{
public Person(string姓名)
{
this.name = name;
}
公共字符串名称;
公共覆盖字符串ToString()
{
return" Name =" +姓名;
}
}
公共级农民:人
{
public Farmer(string name,int acresOfFarmLand)
:base(name)
{
this.acresOfFarmLand = acresOfFarmLand;
}
int acresOfFarmLand;
公共覆盖字符串ToString()
{
返回base.ToString()+" ;;亩= QUOT; +
acresOfFarmLand.ToString();
}
}
}
I had been wondering about that too... Although it can''t cast directly,
you can add each item to a new list..
Can any of the smarter people here explain why this type of generic
inheritance wasn''t included in the language/compiler?
Here''s some sample code... Note in particular the two comments about
halfway down. (Sorry for the bad wrapping - is there a better way to
do this? Should I just attach the file in the future??)
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericTypeInheritanceTest
{
class Program
{
static void Main(string[] args)
{
List<Person> people = new List<Person>();
Person mike = new Person("mike");
Farmer john = new Farmer("john", 10);
Farmer bob = new Farmer("bob", 20);
people.Add(mike);
people.Add(john);
Console.WriteLine("People:");
foreach (Person p in people) Console.WriteLine
(p.ToString());
List<Farmer> farmers = new List<Farmer>();
farmers.Add(john);
farmers.Add(bob);
Console.WriteLine();
Console.WriteLine("Farmers:");
foreach (Farmer f in farmers) Console.WriteLine
(f.ToString());
// Doesn''t compile
// List<Person> otherPeople = new List<Person>
(farmers);
List<Person> otherPeople = new List<Person>();
// But this works just like you would expect!
foreach (Farmer f in farmers) otherPeople.Add(f);
Console.WriteLine();
Console.WriteLine("Other People:");
foreach (Person p in otherPeople) Console.WriteLine
(p.ToString());
Console.ReadLine();
}
}
public class Person
{
public Person(string name)
{
this.name = name;
}
public string name;
public override string ToString()
{
return "Name=" + name;
}
}
public class Farmer : Person
{
public Farmer(string name, int acresOfFarmLand)
: base(name)
{
this.acresOfFarmLand = acresOfFarmLand;
}
int acresOfFarmLand;
public override string ToString()
{
return base.ToString() + "; Acres=" +
acresOfFarmLand.ToString();
}
}
}
这篇关于铸造通用集合&遗产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!