mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-13 02:04:23 +02:00
Added properties (get and set)
This commit is contained in:
@@ -240,7 +240,7 @@ enum HouseSize { // An example of an enum
|
|||||||
|
|
||||||
class Message : GLib.Object { // Class Message extends GLib's Object
|
class Message : GLib.Object { // Class Message extends GLib's Object
|
||||||
private string sender; // a private field
|
private string sender; // a private field
|
||||||
public string text {get; set;} // a public property
|
public string text {get; set;} // a public property (more on that later)
|
||||||
protected bool is_digital = true; // protected (this class and subclasses)
|
protected bool is_digital = true; // protected (this class and subclasses)
|
||||||
internal bool sent = false; // internal (classes in same package)
|
internal bool sent = false; // internal (classes in same package)
|
||||||
|
|
||||||
@@ -302,6 +302,24 @@ public class SignalDemo : GLib.Object {
|
|||||||
// You may use the connect() method and attach as many handlers as you'd like.
|
// You may use the connect() method and attach as many handlers as you'd like.
|
||||||
// They'll all run when the signal is emitted.
|
// They'll all run when the signal is emitted.
|
||||||
|
|
||||||
|
// Properties (getters and setters)
|
||||||
|
|
||||||
|
class Animal : GLib.Object {
|
||||||
|
private int _legs; // prefixed with underscore to prevents name clashes
|
||||||
|
|
||||||
|
public int legs {
|
||||||
|
get { return _legs; }
|
||||||
|
set { _legs = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public int eyes { get; set; default = 5; } // Shorter way
|
||||||
|
}
|
||||||
|
|
||||||
|
rabbit = new Animal();
|
||||||
|
rabbit.legs = 2;
|
||||||
|
rabbit.legs += 2;
|
||||||
|
rabbit.eyes = 2;
|
||||||
|
|
||||||
```
|
```
|
||||||
* More Vala documentation can be found [here](https://valadoc.org/).
|
* More Vala documentation can be found [here](https://valadoc.org/).
|
||||||
* Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw).
|
* Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw).
|
||||||
|
Reference in New Issue
Block a user