C# Polymorphism/Lists

C# Polymorphism/Lists

我在这方面遇到了麻烦。我简单总结一下。

我有 4 个 class。一个是 "Person" class。其他三个是 "Rental",其中有两个 class 继承自 "RentalByDay" 和 "RentalByKM"。

在 "Person" class 中,有一个出租对象列表。我遇到的问题是我不确定如何在创建 Rental 对象时将其添加到该列表中。

class Person
{
    private string _FirstName;
    private string _LastName;

    public Person(string LastName, string FirstName)
    {
        _LastName = LastName;
        _FirstName = FirstName;
    }
    public string LastName
    {
        get { return _LastName; }
    }
    public string FirstName
    {
        get { return _FirstName; }
    }
    public List<Rental> _rentedlist = new List<Rental>();
    public ReadOnlyCollection<Rental> rentedlist
    {
        get { return _rentedlist.AsReadOnly();}
    }  
    public void addrental(Rental Rental)
    {
        _rentedlist.Add(Rental);
    }
    public void PrintRentals()
    {
        foreach (Rental d in _rentedlist)
        {
            Console.WriteLine(d.Person);
        }
    }
}

class Rental
{

    private double _rentduration;

    Person _Person;

    public double rentduration
    {
        get { return _rentduration; }
        set { _rentduration = value; }
    }

    public Person Person
    {
        get { return _Person; }
        set { _Person = value; }
    }


    public Rental(Person Person, double rentduration)
    {

        _Person = Person;
        _rentduration = rentduration;

    }


}

class RentalByDay : Rental
{

    public RentalByDay(Person Person, double rentbydays)
        : base(Person, rentbydays)
    {

        // add to rental list here?

    }

}

class RentalByKm : Rental
{
    public RentalByKm(Person Person, double rentbykm)
        : base(Person, rentbykm)
    {

        // add to rental list here?




    }

}

class RentalAgency
{
    static void Main(string[] args)
    {

        Person jane = new Person("Bloggs", "Jane");
        Person joe = new Person("Bloggs", "Joe");
        Person peter = new Person("Piper", "Peter");
        Person penny = new Person("Piper", "Penny");

        new RentalByDay(jane, 5);

        new RentalByDay(jane, 2);
        jane.PrintRentals();

        new RentalByDay(joe, 8);
        new RentalByKm(joe, 15);
        joe.PrintRentals();

        new RentalByDay(peter, 1);
        new RentalByKm(peter, 85);
        peter.PrintRentals();

        new RentalByDay(penny, 5);
        new RentalByKm(penny, 42);
        penny.PrintRentals();

        Console.WriteLine("Quote for {0}", new RentalByDay(null, 10));
        Console.WriteLine("Quote for {0}", new RentalByKm(null, 10));



    }
}

最终结果应该是,当调用 Printrental 时,将显示该人的所有租金。

如有任何帮助,我们将不胜感激。我觉得这很明显,但无论出于何种原因,我就是想不通。

谢谢!

看这段代码

   new RentalByDay(penny, 5);
   new RentalByKm(penny, 42);
   penny.PrintRentals();

您一直在创建 Rental 子类的新实例,并且没有将这些实例与 Person 的 penny 实例相关联。

您在未命名的 RentalByKm 实例中添加了对 penny 的引用,但没有从 penny 引用 Penny 的租金。当您打印出结果时,您再次创建了一个没有引用任何其他对象的新对象实例。

您实际上需要将那些未命名的引用添加到 penny。

   penny.addrental(new RentalByDay(penny, 5));
   penny.addrental(new RentalByKm(penny, 42));
   penny.PrintRentals();

您需要创建租借,然后将租借添加到用户,而不是尝试创建租借,然后让租借本身添加到用户。

jane.addrental(new RentalByDay(jane, 5));
jane.addrental(new RentalByDay(jane, 2));
jane.PrintRentals();

joe.addrental(new RentalByDay(joe, 8));
joe.addrental(new RentalByKm(joe, 15));
joe.PrintRentals();

peter.addrental(new RentalByDay(peter, 1));
peter.addrental(new RentalByKm(peter, 85));
peter.PrintRentals();

penny.addrental(new RentalByDay(penny, 5));
penny.addrental( new RentalByKm(penny, 42));
penny.PrintRentals();

可以 将租金本身添加到此人,但是,使用 new 创建对象而不将它们分配给任何东西有点不寻常且令人困惑。

public Rental(Person Person, double rentduration)
{
    _Person = Person;
    _rentduration = rentduration;
    _Person.addrental(this);
}

您可能还想为您的 Person class 添加一个 ToString 覆盖,这样当打印到控制台时,您得到的不仅仅是 "Person"。

public override string ToString()
{
    return string.Format("{0}, {1} - {2} rentals.", LastName, FirstName, _rentedlist.Count);
}
public Rental(Person Person, double rentduration)
{

    _Person = Person;
    _rentduration = rentduration;

    Person.addRental(this) // Add this in the base class Constructor. No need to duplicate in each specific

}