[英]How Can I have abstract parameters in method?
Basiclly I have a class like Individual
and another class that inherits from it - IndividualForSomeAnotherWork
. Basiclly我有这样一类Individual
,另一个类,从它继承- IndividualForSomeAnotherWork
。
I have a class called Population
too and methods like Add(Individual individual)
. 我也有一个称为Population
的类和类似Add(Individual individual)
。
Can I pass the IndividualForSomeAnotherWork
to Add method through Individual
type? 我可以通过Individual
类型传递IndividualForSomeAnotherWork
添加方法吗? Or should I use interface or abstract class? 还是应该使用接口或抽象类? I'm asking because I'm getting NullReferenceException
all the time. 我问是因为我一直都在获取NullReferenceException
。
EDIT: Sorry for not answering so long. 编辑:很抱歉没有回答这么久。 My problem was not initializing a List containing objects so I couldn't add to it. 我的问题不是初始化包含对象的列表,所以我无法添加它。 But I also wanted to know that can I pass arguments as I said earlier. 但是我也想知道我可以像我之前所说的那样传递论点。 Thanks for answers. 感谢您的回答。
I would recommend an IIndividual
type of interface. 我会推荐一个IIndividual
类型的接口。 In this case, if you have: 在这种情况下,如果您具有:
abstract class Individual : IIndividual {
}
class IndividualForSomeOtherWork : Individual {
}
... then Population.Add(IIndividual Individual)
will accept the base class Individual
as well as any descendants of Individual
. ...然后Population.Add(IIndividual Individual)
将接受基类的Individual
以及任何后代Individual
。
Think of the interface as a contract with the Population class that any individual within it's collection will have implemented all the functions it requires of the individual. 将接口视为与“人口”类的契约,即该集合中的任何个人都将实现该个人所需的所有功能。
Note that the abstract Individual
is not required to implement all functions defined within the interface. 请注意,不需要抽象的Individual
来实现接口内定义的所有功能。 If the interface requires: 如果界面要求:
interface IIndividual {
void DoWork();
}
... then the base Individual
is not knowledgeable of what specialized work an IndividualForSomeOtherWork
will actually perform. ...然后,基本Individual
不了解IndividualForSomeOtherWork
将实际执行哪些专业工作。 So in the abstract class: 因此,在抽象类中:
abstract void DoWork();
This function must be defined within the specialized individual descendants. 此功能必须在专门的单个后代中定义。
Yes, you can pass an IndividualForSomeAnotherWork
to Add(Individual individual)
. 是的,您可以将IndividualForSomeAnotherWork
传递给Add(Individual individual)
。 It should work correctly. 它应该可以正常工作。 Your error is due to something else. 您的错误是由于其他原因。 Try debugging it yourself, or post more details and code and we might be able to help. 尝试自己调试一下,或者发布更多详细信息和代码,我们可能会提供帮助。
In terms of what you are trying to do here, an abstract class is not fundamentally different from an interface. 就您在此处尝试执行的操作而言,抽象类与接口没有本质上的区别。 In both cases it's not possible to have a argument whose type exactly matches the type of the formal parameter. 在这两种情况下,都不可能有一个类型与形式参数完全匹配的参数。 See DrawImage for an explicit example of this (Image is an abstract class). 有关此内容的显式示例,请参见DrawImage (Image是一个抽象类)。
The NullReferenceException you are seeing is not directly related to the parameter type being abstract. 您看到的NullReferenceException与抽象的参数类型没有直接关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.