Factor Language: Tuple examples
Tuple examples
Factor documentation > Factor handbook > The language > Objects > Tuples
Factor documentation > Factor handbook > The language > Objects > Tuples
Next: | TUPLE: |
An example:
This defines a class word named employee, a predicate employee?, and the following slot accessors:
TUPLE: employee name position salary ;
This defines a class word named employee, a predicate employee?, and the following slot accessors:
이것은 정의한다 하나의 클래스 워드 이름 붙여진 것은 employee, 술어는 employee?, 그리고 그 따르는 slot 접근자들:
We can define a constructor which makes an empty employee:
Reader | Writer | Setter | Changer |
name>> | (>>name) | >>name | change-name |
salary>> | (>>salary) | >>salary | change-salary |
position>> | (>>position) | >>position | change-position |
We can define a constructor which makes an empty employee:
우리는 할 수 있다 정의하다 하나의 생성자 그것은 만든다 하나의 빈 employee:
Or we may wish the default constructor to always give employees a starting salary:
: <employee> ( -- employee ) employee new ;
Or we may wish the default constructor to always give employees a starting salary:
또는 우리는 일 것이다 바라다 그 기본 생성자 나아가서 하는 것은 항상 준다 employee들 어떤 시작 급여:
We can define more refined constructors:
: <employee> ( -- employee ) employee new 40000 >>salary ;
We can define more refined constructors:
우리는 할 수 있다 정의하다 좀 더 개선된 생성자들:
An alternative strategy is to define the most general BOA constructor first:
: <manager> ( -- manager ) <employee> "project manager" >>position ;
An alternative strategy is to define the most general BOA constructor first:
어떤 또다른 전략 이다 나아가서 하는 것은 정의하다 가장 일반적인 BOA 생성자 최초로:
Now we can define more specific constructors:
: <employee> ( name position -- employee ) 40000 employee boa ;
Now we can define more specific constructors:
지금 우리는 할 수 있다 정의하다 좀 더 구체적인 생성자들:
An example using reader words:
: <manager> ( name -- employee ) "manager" <employee> ;
An example using reader words:
한 예 사용하는 읽기 워드들:
TUPLE: check to amount number ; SYMBOL: checks : <check> ( to amount -- check ) checks counter check boa ; : biweekly-paycheck ( employee -- check ) [ name>> ] [ salary>> 26 / ] bi <check> ;
An example of using a changer:
한 예 관련된 것은 사용하는 어떤 changer:
An example using subclassing can be found in Tuple subclassing example.
: positions ( -- seq ) { "junior programmer" "senior programmer" "project manager" "department manager" "executive" "CTO" "CEO" "enterprise Java world dictator" } ; : next-position ( role -- newrole ) positions [ index 1 + ] keep nth ; : promote ( employee -- employee ) [ 1.2 * ] change-salary [ next-position ] change-position ;
An example using subclassing can be found in Tuple subclassing example.
댓글