Vala: EvenNumbers.
This commit is contained in:
parent
3f3c1b1d59
commit
fb277d105c
|
@ -0,0 +1,47 @@
|
|||
public class EvenNumbers {
|
||||
public int get(int index) {
|
||||
return index * 2;
|
||||
}
|
||||
|
||||
public bool contains(int i) {
|
||||
return i % 2 == 0;
|
||||
}
|
||||
|
||||
public string to_string() {
|
||||
return "[This object enumerates even numbers]";
|
||||
}
|
||||
|
||||
public Iterator iterator() {
|
||||
return new Iterator(this);
|
||||
}
|
||||
|
||||
public class Iterator {
|
||||
private int index;
|
||||
private EvenNumbers even;
|
||||
|
||||
public Iterator(EvenNumbers even) {
|
||||
this.even = even;
|
||||
}
|
||||
|
||||
public bool next() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public int get() {
|
||||
this.index++;
|
||||
return this.even[this.index - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
var even = new EvenNumbers();
|
||||
stdout.printf("%d\n", even[5]);
|
||||
if (4 in even) {
|
||||
stdout.printf(@"$even\n");
|
||||
}
|
||||
foreach (var i in even) {
|
||||
stdout.printf("%d\n", i);
|
||||
if (i == 20) break;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue