First NIT release and new clean mercurial repository
[nit.git] / tests / example_point.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 class Point
18 private
19 attr _x: Int # Abscisse
20 attr _y: Int # Ordonnée
21
22
23 meth x: Int
24 do
25 return _x
26 end
27 meth x=(i: Int)
28 do
29 _x = i
30 end
31
32 meth y: Int
33 do
34 return _y
35 end
36 meth y=(i: Int)
37 do
38 _y = i
39 end
40
41 # Change la position d'un point
42 meth moveto(x: Int, y: Int)
43 do
44 _x = x
45 _y = y
46 end
47
48 redef meth to_s: String
49 do
50 var s = "("
51 s.append(_x.to_s)
52 s.add(':')
53 s.append(_y.to_s)
54 s.add(')')
55 return s
56 end
57
58 redef meth ==(p)
59 do
60 return not p is null and p isa Point and _x == p.x and _y == p.y
61 end
62
63
64 init
65 do
66 end
67
68 init at(x: Int, y: Int)
69 do
70 moveto(x, y)
71 end
72
73 end
74
75
76 var p1 = new Point
77 var p2 = new Point.at(5, 8)
78 print(p1) # (0,0)
79 print(p2) # (5,8)
80 print(p1 == p2) # false
81 p1.moveto(5,12)
82 p2.y = 12
83 print(p1 == p2) # true