Classes, Functions and Variables - ValentunSergeev/Eiffel-Docs GitHub Wiki
Classes, Functions and Variables
Content
Classes
Class declaration:
class
APPLICATION
end
Class constructor (add create
keyword with feature name)
class
APPLICATION
create
method_name
feature
method_name(param1: CLASS_NAME; param2: CLASS_NAME)
do
-- do something
end
end
Functions
Command (function with no return value) declaration
feature
-- some code
command
do
-- do something
end
end
Command with parameters
feature
-- some code
command_with_params(param1: INTEGER; param2: STRING)
do
-- do something
end
end
Query (function with the return value)
feature
-- some code
query(param1: CLASS1; param2: CLASS2): RETURN_CLASS
do
-- do something
Result := some_variable -- set return value of the function
end
end
Parameters with the same type can be declared in more compact way
feature
-- some code
fucntion_with_params(param1, param2, param3: INTEGER; param4: STRING)
do
-- do something
end
end
Variables
Variable declaration (non-reference types)
feature
name: STRING
For reference types (STRING + all user-written classes) you must initialize variable in the constructor:
create
init
feature
name: STRING
init
do
name := "Name"
end
You mark variable is detachable
, in this case it will accept Void value (And you do not need to initialize it)
feature
name: detachable STRING
Setter for variable
feature
name: STRING assign set_name
set_name(new_name: STRING)
do
name := new_name
end
Constant declaration
feature
id: INTEGER = 0 -- after that, it cannot be changed
Local variables - using local
keyword:
feature
some_function(param: STRING)
local
local_var: STRING -- note, that you do not need to initialize local variables of reference type
do
local_var := param -- can be used only inside current function
end
Features visibility
To make function/variable accessisble only for certain class(es) use {CLASS1}
or {CLASS1, CLASS2}
:
feature{A, B} -- in this case, name and some_action() will be only accessible inside parent class, A and B
name: STRING
some_action
do
end
feature{NONE} -- in this case, name and some_action() will be only accessible inside parent class
name: STRING
some_action
do
end
You can define different visibility scopes in one class:
class C
feature{A, B} -- only accessible inside parent class, A and B
name: STRING
feature{A} -- only accessible inside parent class and A
age: INTEGER
feature{NONE} -- only accessible inside parent class
age: INTEGER
end