Eiffel Introduction Part IV (1)

zhaozj2021-02-08  466

Eiffel Introduction

Eiffel introduction

Rensselaer, 2000

James C. McKim, JR, Rensselaer At Hartford

K] [N g of @ r k translation

4. Hello World and other examples

4.1 Hello (example)

Class Hello

Creation make

FEATURE

Make IS

- Say Hello to the significant people in the world.

DO

IO.PUTSTRING ("Hello, OOPERS% N")

end

end

Variable IO is a reference, pointing to an object of STANDARD_FILES. Behind We will see how category Hello accesses this variable.

4.2 Eiffel System [Translation 3 / OOSC2E, P196]

l The name of the code file usually should be the same as the names other than the code included, and use .e as the extension.

l So, the category Hello code should be written in a file called Hello.E.

l Each different system should be in different directories.

An example of two systems of Hello and STACK is given below.

4.3 Stack Class

Class my_stack [g]

Creation make

FEATURE

Capacity, Depth: Integer

Push (x: g) IS

- make x The top item.

Require

NOT_FULL: Depth

DO

Depth: = DEPTH 1

S.PUT (x, depth)

Ensure

DEPTH = Old Depth 1

TOP = X

End - push

POP IS

- Remove the top item.

Require

NOT_EMPTY: DEPTH> 0

DO

Depth: = depth - 1

Ensure

Depth = Old Depth - 1

- Top = The item Remaining on the stack (if any)

- That Has Been there The Least Amount of Time.

END - POP

TOP: G IS

- The item That Has Been on The Stack for the Least

- Amount of Time.

Require

NOT_EMPTY: DEPTH> 0

DO

Result: = S.Item (DEPTH)

END - TOP

Feature {none}

S: array [g]

Make (C: Integer)

- Initialize An Empty Stack with Capacity C.

Require

C> 0

DO

Capacity: = C

!! s.make (1, Capacity)

Ensure

Capacity = C

DEPTH = 0

END - MAKE

END - MY_STACK

转载请注明原文地址:https://www.9cbs.com/read-415.html

New Post(0)