创建不同的对象? [英] Creating different objects?

查看:62
本文介绍了创建不同的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我对继承有点新鲜,问题是我正在制作一个应用程序,它应该计算饼干或蛋糕的价格,这取决于他们选择了多少。



我有1个基类和2个子类。 parent class BakeryItem和子类cake和cookie。

但我有点卡在应该创建对象的地方。我不知道在创建用户选择的对象时我应该调用父类或子类。



共有6个类



我以为它可能是这样的?

MainForm

Alright I'm sort of new to inheritence, the thing is I'm making an application thats supposed to calculate the price of either cookies or a cake depending on which they choose an how many.

I have 1 base class and two subclasses. parent class BakeryItem and subclasses cake and cookie.
But I'm kinda stuck in the place where the object is supposed to be created. I don't know wheather I'm supposed to call the parent class or subclass when creating the object of the users choice.

There are 6 classes in total

I thought it might be something like this?
the MainForm

private void button1_Click(object sender, EventArgs e)
   {
       BakeryItem bakeryitem;

       if (ReadInput(out bakeryitem))
       {
           bakeryManager.Add(bakeryitem);
           UpdateGUI();
   }

推荐答案

想一下:你能把BakeryItem转换成蛋糕吗? br />


答案是否定的:BakeryItem可能是一个Cookie,你不能在现实世界中将Cookie转换为Cake,所以你不能这样做它在软件世界中也是! :笑:



当你声明基类的变量(BakeryItem)时,它可以引用从该基类派生的任何类(Cake和Cookie)。但派生类将包含特定于类的信息(例如Cake的IcingColour或Cookie的HasChocolateChips) - 或者为什么该类存在?编译器不能发明IcingColour,或者决定特定的cookie是否有任何巧克力碎片,所以你不能将BakeryItem的实例转换为Cake或Cookie的实例,除非它最初是作为Cake或Cookie开始的。已经有额外的确认。



所以你想做的是:

Think about it for a moment: can you convert a BakeryItem to a Cake?

The answer is no: the BakeryItem could be a Cookie instead, and you can't convert a Cookie to a Cake in the real world, so you can't do it in the software world either! :laugh:

When you declare a variable of your base class (BakeryItem) it can refer to any class which is derived from that base class (Cake and Cookie). But a derived class will contain information that is specific to the class (such as IcingColour for a Cake, or HasChocolateChips for a Cookie) - or why would the class exist? The compiler can't "invent" an IcingColour, or decide if a specific cookie has any chocolate chips, so you can't convert an instance of BakeryItem to an instance of a Cake or Cookie unless it started off life as a Cake or Cookie and so has the extra insofrmation already.

So what you want to do is:
BakeryItem bakeryItem;
if (UserWantsACookie)
   bakeryItem = new Cookie(hasChocolateChips);
else
   bakeryItem = new Cake(Color.Blue);
bakeryManager.Add(bakeryItem);
UpdateGUI();



这是将基类声明为 abstract的一个非常好的主意的原因之一 - 那么编译器将不允许你创建基类的新instacne,只是派生的。


This is one of the reasons that it's a very good idea to declare your base class as abstract - then the compiler will not allow you to cretae a new instacne of the base class, just of derived ones.


我假设ReadInput是这样的,并且你可以做我放在这里的任何一个...



I'm assuming ReadInput is something like this, and you can do either one I put in here...

bool ReadInput(out BakeryItem bakeryItem)
{
    bakeryItem = new Cake();   //Do this one, or
    bakeryItem = new Cookies(12); //You can do this one.

    //...
}





您可以将任何派生类分配给其父项的实例(稍后将其转换回派生类)。



You can assign any derived class to an instance of its parent (and cast it back to the derived class later).


这篇关于创建不同的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆