GNU Smalltalk中Object不理解#init方法及教程页面时效性咨询
Yes, that page is definitely outdated—here's why and how to fix things with your 3.2.91 version of GNU Smalltalk:
The Problem with #init
Older versions of GNU Smalltalk (or examples written for non-standard Smalltalk dialects) might have relied on #init as a default initialization method. But in modern GNU Smalltalk (including your 3.2.91 release), the base Object class does not include a built-in #init method—that’s exactly why you’re hitting the did not understand #init error when running Object new init.
Modern Initialization in GNU Smalltalk
The standard, widely accepted way to initialize objects in GNU Smalltalk (and most mainstream Smalltalk dialects) is using the #initialize method. Here’s how it works smoothly:
- The default
Object class >> newimplementation automatically calls#initializeon the new instance right after creating it. You don’t need to manually write code to trigger the initializer unless you have special custom logic in thenewmethod.
Example of a Proper Custom Class
Object subclass: MyCustomClass [ "Override initialize to add your setup logic" initialize [ "Always call super's initialize first if the parent class defines it" super initialize. Transcript show: 'New MyCustomClass instance initialized'; cr. ] ] "Creating an instance will trigger initialize automatically" MyCustomClass new.
If You Need to Comply with the Old Example
If you specifically want to replicate the outdated page’s pattern, you’ll need to implement the #init method yourself in your custom class (adding it directly to Object is not recommended, as it breaks standard conventions):
Object subclass: OldStyleClass [ OldStyleClass class >> new [ | instance | instance := super new. instance init. ^instance ] init [ "Your initialization code here" Transcript show: 'Old-style init called'; cr. ] ]
Key Takeaway
The page you’re referencing uses deprecated conventions that no longer apply to modern GNU Smalltalk. Stick to #initialize for new code—it’s more portable, aligns with standard Smalltalk practices, and works seamlessly with your current gst version.
内容的提问来源于stack exchange,提问作者user4813927

