classes - Object-Oriented Programming in Sncode.
class Name method Name() endm endc
a = new Name();
Methods are special functions that define the behaviors of your class. A class must have at least one method called the constructor which is named the same as the class itself. This method is where you generally put your initialization tasks.
You can define and use your own methods the same way you would do with functions, but using the keywords
Properties are variables that are bound to an instance of a class. They can be defined and used from any method in the class by using
class Person method Person(age) this.age = age; endm method birthday() this.age++; endm endc
Properties, unlike local variables, are visible from the outside by manipulating the instance of your class like an associative array:
tom = new Person(20); "Tom's age: "; tom.age;
How to call a method on a class instance:
a = new MyClass(); a.myMethod();
How to call a method from another method of a class instance:
class Person // ... method setAge(value) this.age = value; endm method birthday() // like for properties, use 'this' to access methods this.setAge(this.age + 1); endm endc
How to use packages as namespaces for classes:
// declaration package MyPackage class MyClass // ... endc class MySecondClass endc endp // instanciation a = new MyPackage::MyClass();
When you have more then one class in your package, it is recommended to put them in separate files:
package MyPackage class MyClass // ... endc endp
package MyPackage class MySecondClass // ... endc endp
You may now include the class(es) you need:
Written by Jean-Georges Guenard, <firstname.lastname@example.org>Edit
© 2021 extenso Inc. All rights reserved.