繁体   English   中英

C#多态性/列表

[英]C# Polymorphism/Lists

我一直对此感到困扰。 我将简要总结一下。

我有四节课。 一个是“人”类。 其他三个是“ Rental”,从中继承了两个类“ RentalByDay”和“ RentalByKM”。

在“人”类中,有一个租赁对象列表。 我遇到的问题是我不确定如何在创建“出租”对象时将其添加到该列表中。

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类添加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

}

暂无
暂无

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

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