Smalltalk相当于Java的静态是什么? [英] What is the Smalltalk equivalent of Java's static?

查看:233
本文介绍了Smalltalk相当于Java的静态是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Smalltalk相当于Java的静态字段和方法是什么? IOW,Smalltalkers在需要类级数据和/或方法时会做什么?

What is the Smalltalk equivalent of Java's static fields and methods? IOW, what do the Smalltalkers do when they need class level data and/or methods?

推荐答案

我们使用类方法/实例变量。毕竟一个类是一个对象,所以可以有方法。

We use class-side methods/instance variables. A class is an object, after all, so can have methods.

例如,Rectangle类有一个方法#origin:corner:所以你可以写

For instance, the Rectangle class has a method #origin:corner: so you may write

Rectangle origin: 0@0 corner: 100@100

创建一个Rectangle。这只是消息#origin:corner:发送到名为Rectangle的对象(一个类是一个对象!),其中两个Point作为参数。

to create a Rectangle. This is just the message #origin:corner: sent to the object called Rectangle (a class is an object!) with the two Points as parameters.

类侧实例变量的工作方式大致相同。作为对象的类可以具有实例变量。来自SUnit库:

Class-side instance variables work much the same way. A class, being an object, may have instance variables. From the SUnit library:

TestCase class
  instanceVariableNames: 'history'

然后TestCase类以通常的方式暴露这个,使用getter / setter(#history和#history:)。

and then TestCase class exposes this in the usual way, with a getter/setter (#history and #history:).

编辑:我使用的 @ 引起了一些讨论。这就是所谓的二进制消息,它允许用户定义只看其他语言的选择器会打电话给中缀运营商。例如, 3 + 4 ,或 0 @ 0 。在 @ 的情况下, Number 类定义了一个名为 @ 获取参数 y ,定义为 ^ Point x:self y:y - 返回一个Point x坐标是我自己的值,其y坐标是参数。

The @ I used has caused a fair bit of discussion. It's what's called a binary message, which allows one to define selectors that look just what other languages would call infix operators. For instance, 3 + 4, or 0@0. In the case of @, the Number class defines a method called @ taking a parameter y, defined as ^Point x: self y: y - "return a Point whose x coordinate is my own value and whose y coordinate is the parameter".

Point是一个有序对,但当然没有什么可以阻止定义更高维度的版本。 Point 可能会定义一个名为 @ 的方法,例如: ^ Triple x :self xy:self yz:z - 返回R ^ 3中的一个点,其x,y坐标是我自己的,其z坐标是给定参数。

Point is an ordered pair, but of course there's nothing stopping one from defining higher-dimensional versions. Point might define a method called @ that looked like this, for instance: ^Triple x: self x y: self y z: z - "return a point in R^3 whose x, y coordinates are my own, and whose z coordinate is the given parameter".

这篇关于Smalltalk相当于Java的静态是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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