如何创建允许用户在 3 种对象类型之间进行选择的 Windows 界面表单?
所以想法是这样的 - 基类是 Animal,派生类是 Bird 和 Dog。我有一个组合框供用户选择他们想要创建的动物。如何在表单级别将所有这些合并并连接在一起(frmAnimal.cs - 公共部分类 frmAnimal:表单)。
根据此决定,它还会影响表单上的其他按钮,例如调用 .Move() 的按钮(一个重写的 Animal 方法),该按钮将根据选择 Bird 或 Dog 作为对象来调用正确的 Move() 方法。对象类型。
这有道理吗?
So here's the idea - The base class is Animal, and the derived classes are Bird and Dog. I have a ComboBox for the user to choose which animal they'd like to create. How do I incorporate and connect all these all together at the Form level (frmAnimal.cs - public partial class frmAnimal : Form).
Depending upon this decision, it will also affect other buttons on the form, such as a button that calls .Move(), an overriden Animal method, that will call the correct Move() method depending upon whether Bird or Dog is chosen as the object type.
Does that make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想创建并显示一个与选择表单分开的新表单吗?如果是这样,您可以使表单类通用,仅限于动物类型的对象:
然后您可以从该表单派生以创建特定于动物的表单:
如果您想要相同的表单但不同的控件,我将使上述类派生来自用户控件。然后,当用户更改单选按钮时,您可以删除旧动物类型的控件并将其替换为新动物的控件。无论哪种方式,通用祖先 AnimalFormBase 都可以保存与任何 Animal 相关的功能。从技术上讲,它可以包含控件,但我发现设计器呈现的类中的自定义继承可能会变得混乱。然后,这三种派生形式可以使用基本功能,添加自己的功能,并布置每种形式将呈现给用户的控件。
Do you want to create and show a new form separate from the selection form? If so, you could make the form class generic, restricted to objects of an Animal type:
You can then derive from that form to create animal-specific forms:
If you want the same form but different controls, I would make the above classes derive from UserControl. Then, when the user changed the radio button, you could remove and replace the control for the old animal type with the one for the new animal. Either way, the generic ancestor AnimalFormBase can hold functionality pertaining to any Animal. It could technically contain controls, but I've found that custom inheritance in designer-rendered classes can get messy. Then, the three derivative forms could use the base functionality, add their own, and lay out the controls each one would present to the user.
我的例子是在 vb.net 中(我正在学习 C#,但还没有“在那里”——差异还没有那么大,以至于这没有意义,尽管……),但要点是通用的。您有一个基类 Animal 和一些派生自 Animal 的类。在基类上定义 .Move 方法。然后创建派生类,它继承基类的方法和公共接口。定义 AnimalType 属性以区分动物的类型。对于这个非常简单的示例,我使用了枚举。实际上,这可能是一个枚举,也可能是一个数据库 PK,或其他唯一标识符。请注意,在实践中,我可能不会使 superType Animal 成为与派生类相同的枚举的成员。例如,在数据库中,我可能会将派生对象设为与“父”动物记录相关的“子”记录):
现在您的派生类:
现在是一个 AnimalFactory 类,它生成动物实例和从类型派生的类动物,作为(再一次......类型动物):
这里的技巧是,由 AnimalFacorty 返回的派生类是动物类型,但实现了其真实类型的所有属性,如果需要的话,可以转换成他们的真实类型。现在,你的表格。如果您仅访问基类上定义的通用方法,那么您的表单也不必关心返回的动物类型。如果您需要访问特定于派生类的方法,则需要执行转换操作(这在 C# 中与 VB.net 中可能相同(或非常相似)。
我以这种形式抛出了一些非常愚蠢的示例也可能有一些方法可以避免 Select Case(我相信您会知道它是 Switch 语句),但我不知道您在使用代码做什么,所以这只是一个学术练习。
My example is in vb.net (I am working on learning C#, but not "there" yet - the differences are not so great that this won't make sense, though . . .), but the essentials are failry universal. You have a base class Animal, and some classes that derive from animal. Define the .Move method on your base class. Then create your derived classes, which inherit the methods and public interface of your base class. Define an AnimalType Property in order to differentiate types on animal. FOr this very simple example, I used an Enum. In reality, this might be an Enum, or it could be a Database PK, or other unique identifier. Note that in practice, I would probably NOT make the superType Animal a member of the same enumeration as the derived classes. In a DataBase I would probably Make the derived objects "child" records related to a "Parent" animal record, for example):
Now your Derived Classes:
Now, an AnimalFactory class, which cranks out instances of animal and classes which derive from type animal, as (one more time . . . type animal):
The trick here is that the derived classes, as returned by the AnimalFacorty, are of type animal, but the implement all the propeties of thier true type, and if need be, can be cast to their true type. Now, your Form. Your form need not necessarily care which type of animal in returned either, if you are only accessing common methods defined on the base class. If you need to access a method specific to a derived class, you will need to perform a casting operation (which chould be the same (or very similar) in C# as in VB.net.
I have thrown some really silly examples in this form. There are also probably ways to avoid the Select Case (What I believe you would know as a Switch statement), but I have no idea what you are doing with your code, so this is simply an acedmic excercise . . .