Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#多态性/列表_C#_List_Class_Object_Polymorphism - Fatal编程技术网

C#多态性/列表

C#多态性/列表,c#,list,class,object,polymorphism,C#,List,Class,Object,Polymorphism,我在这方面遇到了麻烦。我将简要总结一下 我有四节课。一个是“人”类。另外三个是“Rental”,继承了“RentalByDay”和“RentalByKM”两个类 在“Person”类中,有一个租赁对象列表。我遇到的问题是,我不知道如何在创建租赁对象时将其添加到该列表中 class Person { private string _FirstName; private string _LastName; public Person(string LastName, str

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

我有四节课。一个是“人”类。另外三个是“Rental”,继承了“RentalByDay”和“RentalByKM”两个类

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

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));



    }
}
班级人员
{
私有字符串_FirstName;
私有字符串_LastName;
公众人物(字符串LastName、字符串FirstName)
{
_LastName=LastName;
_名字=名字;
}
公共字符串姓氏
{
获取{return\u LastName;}
}
公共字符串名
{
获取{return\u FirstName;}
}
公共列表_rentedlist=新列表();
公共只读集合出租列表
{
获取{return _rentedlist.AsReadOnly();}
}  
公共租赁(租赁)
{
_rentedlist.添加(租赁);
}
公共租赁
{
foreach(租赁清单中的租赁)
{
控制台。写线(d.Person);
}
}
}
班级租金
{
私人双倍租金期限;
人(人),;
公共双倍租期
{
获取{return\u rentduration;}
设置{u rentduration=value;}
}
公众人物
{
获取{return\u Person;}
设置{u Person=value;}
}
公共租赁(人,双倍租赁期限)
{
_人=人;
_租期=租期;
}
}
班租按天:租金
{
公共租赁日(人,双倍租赁日)
:基数(人,租用天数)
{
//在这里添加到租赁列表?
}
}
类别:租赁BYKM:租赁
{
公共租赁公里数(人-人,双倍租赁公里数)
:基数(人,每公里租金)
{
//在这里添加到租赁列表?
}
}
类租金代理
{
静态void Main(字符串[]参数)
{
Person jane=新人(“Bloggs”、“jane”);
Person joe=新人(“Bloggs”、“joe”);
人物彼得=新人(“吹笛者”、“彼得”);
佩妮=新人(“风笛手”、“佩妮”);
新租金日(简,5岁);
新租赁日(简,2岁);
jane.PrintRentals();
新租赁日(乔,8岁);
新RentalByKm(乔,15岁);
joe.PrintRentals();
新租金日(彼得,1);
新RentalByKm(彼得,85岁);
彼得·普林特尔斯();
每日新租金(便士,5);
新租金(42便士);
佩妮。打印租赁公司();
WriteLine({0}的引号),new RentalByDay(null,10));
WriteLine({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

}