简体   繁体   English

Smalltalk:是否有“在”或“被包含”之类的东西?

[英]Smalltalk: Is there something like “is in” or “is contained”?

I'm having trouble with Smalltalk.我在使用 Smalltalk 时遇到问题。 Is there some operator like "is in" or "is contained / included"?是否有诸如“在”或“包含/包含”之类的运算符?

I have classes Student and Exam (with attribute student) and collections StudentsList and ExamsList.我有学生和考试课程(具有学生属性)和 collections StudentsList 和 ExamsList。 In the ExamsList, I would like to show all instances of class Exam that meet the criteria that their student (object set as value of attribute student) is contained in collection StudentsList.在 ExamsList 中,我想显示符合条件的 class 考试的所有实例(对象设置为属性 student 的值)包含在学生列表中。

Something like the following code, but it does not work:类似于以下代码,但它不起作用:

ExamsList list: (Exam allInstances select: [:ex | (StudentsList includes: ex student) ]).

Could you think of some elegant solution?你能想出一些优雅的解决方案吗?

Many thanks!非常感谢!

Even though your question lacks some information, there are a few comments that might help.即使您的问题缺少一些信息,也有一些评论可能会有所帮助。

  1. If your naming convention is consistent and ExamsList is a class, then StudentsList must be a class too.如果您的命名约定是一致的并且ExamsList是 class,那么StudentsList也必须是 class。 In that case your code doesn't work because classes do not understand the message #includes: , which is intended to be sent to subinstances of Collection .在这种情况下,您的代码不起作用,因为类不理解消息#includes: ,该消息旨在发送到Collection的子实例。

  2. Assuming my guess is applicable, I would point out that it is not a good idea to have a class for every collection of objects.假设我的猜测是适用的,我会指出,对于每个对象集合都有一个 class 并不是一个好主意。 So, instead of having the class ExamsList , you should add a class variable Exams to the class Exam , initialized to a Set and store there every instance of Exam that you want to preserve for future queries.因此,您应该将 class 变量Exams添加到 class Exam中,而不是使用 class ExamsList ,初始化为Set并存储您想要保留的每个Exam实例。

  3. In the same way, you should add a class variable Students to the class Student and get rid of StudentsList .同样,您应该将 class 变量Students添加到 class Student并删除StudentsList

  4. With this design every new instance that matters should be saved in the corresponding collection held by the class (see 6 below for a hint on how to do this).通过这种设计,每个重要的新实例都应保存在 class 持有的相应集合中(有关如何执行此操作的提示,请参见下面的 6)。 This would eliminate the need for enumerating #allInstances .这将消除枚举#allInstances的需要。

  5. In any case, you should understand that #allInstances is a system message, ie, it is not intended for querying objects in the realm of your model as it belongs in much lower level of abstraction.在任何情况下,您都应该了解#allInstances是一条系统消息,即它不适用于查询 model 的 realm 中的对象,因为它属于低得多的抽象级别。 Note that #allInstances will collect instances that for whatever reason (tests, examples, open debuggers or inspectors, etc.) may still be around without being part of your model.请注意, #allInstances将收集无论出于何种原因(测试、示例、打开的调试器或检查器等)可能仍然存在而不是 model 的一部分的实例。

  6. If every Exam has a Student , you could store an Exam in the Exams collection whenever you assign it to the designated Student , something on the lines of如果每个Exam都有一个Student ,那么您可以在将Exam分配给指定的Student时将其存储在Exams集合中,类似于

   Exam >> forStudent: aStudent
     Exams add: self.
     student := aStudent

( student is an ivar of Exam , and self represents the instance with concrete questions) studentExam的 ivar, self代表具体问题的实例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM