时间:2019-03-17 标签:c#abstractclasses
我必须创建一个假的 DMV 程序来计算商用和私人车辆的年费。这两个类将是抽象的,并且它们将来自名为“所有车辆”的类的多态性。
我的老师只希望一个对象创建整个程序(在主程序中),但因为我的前 3 层类是抽象的。我无法用它们创建对象,即 cars = new cars();
所以我的问题是我如何只创建一个对象,因为它们是抽象的?如果您有任何疑问,请随时提问,我可能没有解释清楚......
i have to create a fake DMV program that calculates annual fees, for commercial and private vehicles. Those two classes will be abstract and they will polymophism from a class named all vehicles.
My instructor wants only one object created the entire program(in main) but since my top 3 tier classes are abstract. I can't create an object with them i.e. vehicles = new vehicles();
so my question is how do i create only one object since they are abstract? If you have any questions feel free to ask, I might have not explained this well...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许您要创建一个代表 DMV 的对象,但该对象的定义包括其他对象的实例。
Maybe you are to make one object which represents the DMV, but the definition of that object includes instances of other objects.
您的讲师可能希望您通过对抽象基类的引用来实例化多个对象:
即您有一个对象引用(传送),其行为取决于您实例化的具体类型的多态性。
Your instructor may want you to instantiate multiple objects through a reference to the abstract base class:
i.e. you have a single object reference (conveyance) that behaves polymorphically depending on the concrete type you've instantiated.
您的班级结构将类似于:
对于每个班级,依此类推。在
Main
方法中,您将根据输入创建实例,但实际变量将被声明为Vehicle
类型。实际计算将根据您创建的实例生效:Your class structure will look something like:
and so on, for each class. In your
Main
method, you would create the instance based on input, but your actual variable would be declared as typeVehicle
. The actual calculation will take effect based on the instance that you create:你似乎有点困惑。 “所有车辆”应该是抽象的。 “商用车”和“私家车”不应该是抽象的,除非这两者有具体的子类。
您也可能不理解您的老师所说的“只有一个对象”的含义,因为这没有意义。
You seem to be a bit confused. "All Vehicles" should be abstract. "Commercial Vehicle" and "Private Vehicle" should not be abstract, unless there are concrete subclasses of those two.
You may also not be understanding what your instructor means by "only one object", since that doesn't make sense.