简体   繁体   English

如何在方法中使用抽象参数?

[英]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.

相关问题 C# - 在一个方法中,我怎样才能有 2 个参数是一个? - C# - In a method, how can I have 2 parameters that are one? 为什么我可以抽象覆盖抽象方法? - Why can I abstract override an abstract method? 为什么抽象类可以有一个实例方法? - why abstract class can have a instance method? 如何在 [Setup] 或 [OneTimeSetup] 中传递参数,以便我不必在测试类中调用该方法? - How can I pass parameters in a [Setup] or [OneTimeSetup] so I so not have call the method in my test class? 如何在一个类中同时具有抽象方法和虚拟方法? - How can I have both abstract and virtual methods in one class? 我怎样才能有条件输出参数 - How can I have conditional out parameters 如何使抽象方法返回具有具体实现的抽象类型? - How to have an abstract method return an abstract type with concrete implementations? 我可以使用invoke抽象类方法吗 - can I use invoke to abstract class method 如何使用具有不同构造函数参数的接口实现的抽象工厂? - How to use an abstract factory with realizations of an interface that have different constructor parameters? 抽象方法中的可选参数? 可能吗? - Optional Parameters in Abstract method? Is it possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM