简体   繁体   English

如何将抽象类作为参数传递给类型的实例?

[英]How do I pass abstract class as parameter for instance of type?

So I have a type called FunBond , with a default constructor like this 所以我有一个名为FunBond的类型,它有一个像这样的默认构造函数

public FunBond(int id,
            string name,
            Currency currency,
            double notional,
            DateTime maturityDate,
            List<CashFlowWithDate> couponCashFlows,
            DayCounter dayCounter)
            : base(id, name, notional, InstrumentType.FixedRateBond,  currency, maturityDate, dayCounter)
{ 
...
}

And DayCounter is an abstract class DayCounter是一个抽象类

public abstract class DayCounter
{
    public abstract string Name();
    public abstract double YearFraction(DateTime d1, DateTime d2);
    public abstract int DayCount(DateTime d1, DateTime d2);
}

Now my question is, what am I supposed to supply as a DayCounter to generate an instance of a FunBond ? 现在的问题是,我应该怎样提供的DayCounter生成一个实例FunBond I can specify the id, name, currency, ... etc. etc. to pass as parameters to FunBond , but what is DayCounter supposed to be? 我可以指定id,name,currency,...等作为参数传递给FunBond ,但DayCounter应该是什么? I can't create an instance of it, and I can't supply it with anything I don't think... I thought I would need another class deriving from my abstract class to provide to FunBond , so I think I misunderstand something fundamentally. 我无法创建它的实例,我无法提供任何我不认为的东西......我想我需要另一个派生自我的抽象类的类来提供给FunBond ,所以我想我误解了一些东西从根本上。

The constructor is declaring that it requires an parameter of type DayCounter . 构造函数声明它需要DayCounter类型的参数。 Since DayCounter is an abstract class then you need to pass an instance of a class that derives from DayCounter . 由于DayCounter是一个抽象类,因此您需要传递一个派生自DayCounter的类的实例。

That does not mean that you need to change the constructor parameter type, just pass an instance of a derived type. 这并不意味着您需要更改构造函数参数类型,只需传递派生类型的实例。

The reason that someone defines a parameter of an abstract type is to allow polymorphism and loose coupling. 有人定义抽象类型的参数的原因是允许多态和松散耦合。 You can have different derived classes of DayCounter , each behaving differently (as long as they adhere to the contract of DayCounter ). 您可以使用不同的DayCounter派生类,每个类的行为都不同(只要它们符合DayCounter合约 )。 And FunBond can speak to instances of such classes without knowing how they internally work. FunBond可以在不知道内部工作方式的情况下与这些类的实例交谈。 As far as FunBond is concerned, it is speaking to an object that adheres to the contract of DayCounter , but doesn't care how it internally implemented. FunBond而言,它是指一个遵守DayCounter合约的DayCounter ,但不关心它是如何在内部实现的。

The question says: 问题是:

I thought I would need another class deriving from my abstract class to provide to FunBond 我以为我需要另一个派生自我的抽象类的类来提供给FunBond

That is correct - you need to create a concrete class from the abstract. 这是正确的 - 你需要从摘要中创建一个具体的类。

// Your derived class
public class WeekdayCounter : DayCounter
{
    public override string Name() { return "Weekday" }
    public override double YearFraction(DateTime d1, DateTime d2) { return 0.0; }
    public override int DayCount(DateTime d1, DateTime d2) { return 0; }
}

// Create a FunBond
WeekdayCounter c = new WeekdayCounter();  // **derived class**

FunBond yay = new FunBond(id,
                          name,
                          currency,
                          notional,
                          maturityDate,
                          couponCashFlows,
                          c);   // **WeekdayCounter, which is a DayCounter**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM