用户创建的新课程?
考虑这种情况:我有一个水族馆模拟器,里面有 5 种不同类型的鱼。不同的类型意味着不同的属性(速度、颜色、饥饿度等)。如果我希望模拟器的用户能够创建一种新型鱼并为其赋予属性值,该怎么办?
程序员是如何实现的呢?我是否需要某种“事件处理”来在我的“Fish”类中添加一组特定的代码行?这是一个正确的想法吗?
(如果有必要的话,语言是 Java。为了避免任何误解并防止出现诸如“这个大学可以工作吗?”之类的评论,是的,可以。但我不是在寻找答案,我对这个概念很好奇。)
< strong>编辑:是的,我很遗憾我没有提到交互方式:GUI。
因此,想象一个名为“添加新物种”的选项卡,其中包含鱼类的每个属性(类型、速度、颜色等)的字段。因此,用户在字段中填写适当的值,当他单击“添加”时,将调用构造函数。至少我是这么想的。 :)
Consider this situation: I've got an aquarium simulator where I have 5 different types of fishes. Different types means different attributes (speed, colour, hunger, etc). What if I want the user of my simulator to be able to create a new type of fish and give it its values for its attributes?
How is that implemented by the programmer? Do I need some kind of "event handling" that will add a specific bunch of lines of code in my "Fish" class? Is that even a valid thought?
(In case it's essential, the language is Java. And to avoid any misunderstandings and prevent comments like "is this uni work?", yes it is. But I am not looking for THE answer, I am curious about the concept.)
EDIT: Yeah, my bad that I didn't mention the interaction way: a GUI.
So, imagine a tab called "Add New Species" that has a field for every attribute of the fishes (type, speed, colour, etc). So, the user fills in the fields with the appropriate values and when he clicks on "add" the constructor is called. At least that's how I imagine it. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我只想使用地图:
I would just use a map:
Java 是一种面向对象的语言,它处理类和对象。诱人的、幼稚的解决方案是让你的程序像处理任何东西的类一样处理鱼的“类”,即创建一些 Java 代码并让编译器和加载器将其引入运行时。
这种方法可以发挥作用,但有些尴尬。本质上,您的“动态 Java 类”编码最终可能会比您的作业实际预期的更大、更复杂。
仅当您实际上要为不同的鱼拥有不同的属性(不仅仅是这些属性的不同值)时,您才真正需要这样做;即使如此,也有更简单的解决方案。
对于所问的问题,我认为您实际上只需要一个
Fish
类。当用户定义一个新属性时,他真正定义的是属性值。如果您确实想要新的动态属性,那么您可以使用例如 HashMap 来存储名称/值对。您可以让用户添加“legs”/“4”,然后按原样打印出该新属性;但你无法让鱼用这些腿行走,因为你会缺少使用新属性的编码。
Java is an OO language, and it deals in classes and objects. The tempting, naive solution would be to have your program deal with "classes" of fish like it deals with classes of anything, i.e. to create some Java code and let the compiler and loader introduce it into the runtime.
This approach can be made to work, with some awkwardness. Essentially your "dynamic Java classes" coding would probably end up much bigger and complicated than your assignment actually intends.
You only really need to do this if you are actually going to have different attributes (not just different values of those attributes) for your different fish; and even then there are simpler solutions.
For what's being asked, I think you really only need one
Fish
class. When the user defines a new one, what he's really defining are the attribute values.If you really want new and dynamic attributes, then you could go a long way using e.g. a
HashMap
to store name/value pairs. You could let the user add"legs" / "4"
and then print out that new attribute as-is; but you couldn't make the fish walk on those legs because you'd be missing coding to work with the new attribute.查看类型对象模式。另外,谷歌搜索我刚刚给出了我找到的第一个参考文献之一...
您也可以查看 反射模式...
Have a look at the type object pattern. Also google for it I just gave one of the first references I found...
You may also look the Reflection pattern...
让用户定义 FishSpecies 类实例的属性值,并为 FishSpecies 提供一个 createFish 方法,该方法创建该物种的鱼(即具有这些属性值)。跟踪列表中的所有 FishSpecies 对象使您有机会管理 FishSpecies 对象并创建给定物种的 Fish 对象。
如果我正确理解你的问题,那么我相信让事情变得更加复杂是一个错误。
Let the user define attribute values of an instance of, say, a FishSpecies class, and give the FishSpecies a method createFish that creates a fish of that species (i.e. having those attribute values). Keeping track of all FishSpecies objects in a list grants you the opportunity to manage FishSpecies objects, and create Fish objects of given species.
If I understand your question correctly, then I believe that complicating things more than this is a mistake.