File: getters_setters.ss

package info (click to toggle)
surgescript 0.5.4.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,876 kB
  • sloc: ansic: 13,674; makefile: 16
file content (62 lines) | stat: -rw-r--r-- 1,232 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// getters_setters.ss
// getters & setters in SurgeScript
// Copyright 2017 Alexandre Martins <alemartf(at)gmail(dot)com>
//

object "Application"
{
    obj = spawn("Skull");

    state "main"
    {
        hello();
        showStatus();
        obj.eyes = 4; // the same as obj.set_eyes(4)
        //obj.name = "master"; // will crash; no setter defined. Try uncommenting this.
        showStatus();
        Application.exit();
    }

    fun hello()
    {
        // typing obj.name is the same as obj.get_name()
        Console.print("Hello, " + obj.name);
    }

    fun showStatus()
    {
        Console.print("Skull " + obj.name + " has " + obj.eyes + " eyes.");
    }
}

object "Skull"
{
    name = "kid";
    eyes = 2;

    state "main"
    {
    }

    // get_eyes()
    // Note: typing obj.eyes yields the same as obj.get_eyes()
    fun get_eyes()
    {
        return eyes;
    }

    // set_eyes()
    // Syntactic sugar to obj.eyes = value (will call obj.set_eyes(value) behind the scenes)
    fun set_eyes(value)
    {
        eyes = value;
    }

    // get_name()
    // obj.name will return the [private] variable name. Note that we did not define a setter.
    fun get_name()
    {
        return name;
    }
}