正在加载图片...
$18.2 USE OF CONSTANTS 645 Like other attributes,constant attributes are either exported or secret;if they are exported,clients of the class may access them through feature calls.So if C is the class containing the above declarations and x,declared of type C,has a non-void value,then x.Backslash denotes the backslash character. Unlike variable attributes,constant attributes do not occupy any space at run time in instances of the class.So there is no run-time penalty for adding as many constant attributes as you need. 18.2 USE OF CONSTANTS Here is an example showing how clients may use constant attributes defined in a class: class FILE feature error code:INTEGER: --Variable attribute Ok:INTEGER is 0 Open_error:INTEGER is 1 open (file name:STRING)is --Open file of name file name --and associate it with current file object do error code:=Ok if“Something went wrong”then error code:=Open error end end ..Other features... end A client may call open and compare the resulting error code to any of the constants to test how the operation went: 大FILE,… f.open if f.error code =f.Open error then “Appropriate action else end Often,however,a group of constants is needed without being attached to any particular object.For example,a system performing physics computations may use some numerical constants;or a text editor may need character constants describing the character§18.2 USE OF CONSTANTS 645 Like other attributes, constant attributes are either exported or secret; if they are exported, clients of the class may access them through feature calls. So if C is the class containing the above declarations and x, declared of type C, has a non-void value, then x ● Backslash denotes the backslash character. Unlike variable attributes, constant attributes do not occupy any space at run time in instances of the class. So there is no run-time penalty for adding as many constant attributes as you need. 18.2 USE OF CONSTANTS Here is an example showing how clients may use constant attributes defined in a class: class FILE feature error_code: INTEGER; -- Variable attribute Ok: INTEGER is 0 Open_error: INTEGER is 1 … open (file_name: STRING) is -- Open file of name file_name -- and associate it with current file object do error_code := Ok … if “Something went wrong” then error_code := Open_error end end … Other features … end A client may call open and compare the resulting error code to any of the constants to test how the operation went: f: FILE; … f ● open if f ● error_code = f ● Open_error then “Appropriate action” else … end Often, however, a group of constants is needed without being attached to any particular object. For example, a system performing physics computations may use some numerical constants; or a text editor may need character constants describing the character
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有